Skip to content

Commit 8ed6b21

Browse files
committed
Add -type=library+program and -type=program+library
a.k.a. -type=both and -type=combined respectively, for packages containing both library and program, see #814690. Fixes #57
1 parent adc2f74 commit 8ed6b21

File tree

3 files changed

+195
-84
lines changed

3 files changed

+195
-84
lines changed

make.go

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ import (
1717
"golang.org/x/tools/go/vcs"
1818
)
1919

20+
type packageType int
21+
22+
const (
23+
typeGuess packageType = iota
24+
typeLibrary
25+
typeProgram
26+
typeLibraryProgram
27+
typeProgramLibrary
28+
)
29+
2030
var (
2131
dep14 bool
2232
pristineTar bool
@@ -388,39 +398,40 @@ func normalizeDebianProgramName(str string) string {
388398
}
389399

390400
// This follows https://fedoraproject.org/wiki/PackagingDrafts/Go#Package_Names
391-
func debianNameFromGopkg(gopkg, t string, allowUnknownHoster bool) string {
401+
func debianNameFromGopkg(gopkg string, t packageType, allowUnknownHoster bool) string {
392402
parts := strings.Split(gopkg, "/")
393403

394-
if t == "program" {
404+
if t == typeProgram || t == typeProgramLibrary {
395405
return normalizeDebianProgramName(parts[len(parts)-1])
396406
}
397407

398408
host := parts[0]
399-
if host == "github.com" {
409+
switch host {
410+
case "github.com":
400411
host = "github"
401-
} else if host == "code.google.com" {
412+
case "code.google.com":
402413
host = "googlecode"
403-
} else if host == "cloud.google.com" {
414+
case "cloud.google.com":
404415
host = "googlecloud"
405-
} else if host == "gopkg.in" {
416+
case "gopkg.in":
406417
host = "gopkg"
407-
} else if host == "golang.org" {
418+
case "golang.org":
408419
host = "golang"
409-
} else if host == "google.golang.org" {
420+
case "google.golang.org":
410421
host = "google"
411-
} else if host == "bitbucket.org" {
422+
case "bitbucket.org":
412423
host = "bitbucket"
413-
} else if host == "bazil.org" {
424+
case "bazil.org":
414425
host = "bazil"
415-
} else if host == "pault.ag" {
426+
case "pault.ag":
416427
host = "pault"
417-
} else if host == "howett.net" {
428+
case "howett.net":
418429
host = "howett"
419-
} else if host == "go4.org" {
430+
case "go4.org":
420431
host = "go4"
421-
} else if host == "salsa.debian.org" {
432+
case "salsa.debian.org":
422433
host = "debian"
423-
} else {
434+
default:
424435
if allowUnknownHoster {
425436
suffix, _ := publicsuffix.PublicSuffix(host)
426437
host = host[:len(host)-len(suffix)-len(".")]
@@ -574,11 +585,15 @@ func execMake(args []string, usage func()) {
574585
false,
575586
"Keep using a pristine-tar branch as in the old workflow.\nStrongly discouraged, see \"pristine-tar considered harmful\"\n https://michael.stapelberg.ch/posts/2018-01-28-pristine-tar/\nand the \"Drop pristine-tar branches\" section at\nhttps://go-team.pages.debian.net/workflow-changes.html")
576587

577-
var pkgType string
578-
fs.StringVar(&pkgType,
588+
var pkgTypeString string
589+
fs.StringVar(&pkgTypeString,
579590
"type",
580591
"",
581-
"One of \"library\" or \"program\"")
592+
"Set package type, one of:\n"+
593+
` * "library" (aliases: "lib", "l", "dev")`+"\n"+
594+
` * "program" (aliases: "prog", "p")`+"\n"+
595+
` * "library+program" (aliases: "lib+prog", "l+p", "both")`+"\n"+
596+
` * "program+library" (aliases: "prog+lib", "p+l", "combined")`)
582597

583598
fs.StringVar(&wrapAndSort,
584599
"wrap-and-sort",
@@ -608,9 +623,33 @@ func execMake(args []string, usage func()) {
608623
gopkg = rr.Root
609624
}
610625

611-
debsrc := debianNameFromGopkg(gopkg, "library", allowUnknownHoster)
612-
613-
if strings.TrimSpace(pkgType) != "" {
626+
// Set default source and binary package names.
627+
// Note that debsrc may change depending on the actual package type.
628+
debsrc := debianNameFromGopkg(gopkg, typeLibrary, allowUnknownHoster)
629+
debLib := debsrc + "-dev"
630+
debProg := debianNameFromGopkg(gopkg, typeProgram, allowUnknownHoster)
631+
632+
var pkgType packageType
633+
634+
switch strings.TrimSpace(pkgTypeString) {
635+
case "", "guess":
636+
pkgType = typeGuess
637+
case "library", "lib", "l", "dev":
638+
pkgType = typeLibrary
639+
case "program", "prog", "p":
640+
pkgType = typeProgram
641+
case "library+program", "lib+prog", "l+p", "both":
642+
// Example packages: golang-github-alecthomas-chroma,
643+
// golang-github-tdewolff-minify, golang-github-spf13-viper
644+
pkgType = typeLibraryProgram
645+
case "program+library", "prog+lib", "p+l", "combined":
646+
// Example package: hugo
647+
pkgType = typeProgramLibrary
648+
default:
649+
log.Fatalf("-type=%q not recognized, aborting\n", pkgTypeString)
650+
}
651+
652+
if pkgType != typeGuess {
614653
debsrc = debianNameFromGopkg(gopkg, pkgType, allowUnknownHoster)
615654
if _, err := os.Stat(debsrc); err == nil {
616655
log.Fatalf("Output directory %q already exists, aborting\n", debsrc)
@@ -644,19 +683,15 @@ func execMake(args []string, usage func()) {
644683
log.Fatalf("Could not create a tarball of the upstream source: %v\n", err)
645684
}
646685

647-
if strings.TrimSpace(pkgType) == "" {
686+
if pkgType == typeGuess {
648687
if u.firstMain != "" {
649688
log.Printf("Assuming you are packaging a program (because %q defines a main package), use -type to override\n", u.firstMain)
650-
pkgType = "program"
689+
pkgType = typeProgram
651690
debsrc = debianNameFromGopkg(gopkg, pkgType, allowUnknownHoster)
652691
} else {
653-
pkgType = "library"
692+
pkgType = typeLibrary
654693
}
655694
}
656-
debbin := debsrc + "-dev"
657-
if pkgType == "program" {
658-
debbin = debsrc
659-
}
660695

661696
if _, err := os.Stat(debsrc); err == nil {
662697
log.Fatalf("Output directory %q already exists, aborting\n", debsrc)
@@ -692,7 +727,7 @@ func execMake(args []string, usage func()) {
692727
for _, dep := range u.repoDeps {
693728
if len(golangBinaries) == 0 {
694729
// fall back to heuristic
695-
debdependencies = append(debdependencies, debianNameFromGopkg(dep, "library", allowUnknownHoster)+"-dev")
730+
debdependencies = append(debdependencies, debianNameFromGopkg(dep, typeLibrary, allowUnknownHoster)+"-dev")
696731
continue
697732
}
698733
bin, ok := golangBinaries[dep]
@@ -703,7 +738,7 @@ func execMake(args []string, usage func()) {
703738
debdependencies = append(debdependencies, bin)
704739
}
705740

706-
if err := writeTemplates(dir, gopkg, debsrc, debbin, debversion, pkgType, debdependencies, u.vendorDirs); err != nil {
741+
if err := writeTemplates(dir, gopkg, debsrc, debLib, debProg, debversion, pkgType, debdependencies, u.vendorDirs); err != nil {
707742
log.Fatalf("Could not create debian/ from templates: %v\n", err)
708743
}
709744

@@ -715,6 +750,20 @@ func execMake(args []string, usage func()) {
715750
log.Printf("\n")
716751
log.Printf("Packaging successfully created in %s\n", dir)
717752
log.Printf("\n")
753+
log.Printf(" Source: %s\n", debsrc)
754+
switch pkgType {
755+
case typeLibrary:
756+
log.Printf(" Package: %s\n", debLib)
757+
case typeProgram:
758+
log.Printf(" Package: %s\n", debProg)
759+
case typeLibraryProgram:
760+
log.Printf(" Package: %s\n", debLib)
761+
log.Printf(" Package: %s\n", debProg)
762+
case typeProgramLibrary:
763+
log.Printf(" Package: %s\n", debProg)
764+
log.Printf(" Package: %s\n", debLib)
765+
}
766+
log.Printf("\n")
718767
log.Printf("Resolve all TODOs in %s, then email it out:\n", itpname)
719768
log.Printf(" sendmail -t < %s\n", itpname)
720769
log.Printf("\n")

make_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func TestNormalizeDebianProgramName(t *testing.T) {
4646

4747
var nameFromGoPkg = []struct {
4848
in string
49-
t string
49+
t packageType
5050
out string
5151
}{
52-
{"github.com/dh-make-golang", "program", "dh-make-golang"},
53-
{"github.com/DH-make-golang", "", "golang-github-dh-make-golang"},
54-
{"github.com/dh_make_golang", "", "golang-github-dh-make-golang"},
52+
{"github.com/Debian/dh-make-golang", typeProgram, "dh-make-golang"},
53+
{"github.com/Debian/DH-make-golang", typeGuess, "golang-github-debian-dh-make-golang"},
54+
{"github.com/Debian/dh_make_golang", typeGuess, "golang-github-debian-dh-make-golang"},
5555
}
5656

5757
func TestDebianNameFromGopkg(t *testing.T) {

0 commit comments

Comments
 (0)