Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Add support for compiling single files #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions go.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func GoCrossCompile(packagePath string, platform Platform, outputTpl string, ldf
return err
}
tplData := OutputTemplateData{
Dir: filepath.Base(packagePath),
// If compiling a file, use that file's name without extension.
// Otherwise use the package name
Dir: filepath.Base(strings.TrimSuffix(packagePath, ".go")),
OS: platform.OS,
Arch: platform.Arch,
}
Expand All @@ -52,9 +54,9 @@ func GoCrossCompile(packagePath string, platform Platform, outputTpl string, ldf

// Go prefixes the import directory with '_' when it is outside
// the GOPATH.For this, we just drop it since we move to that
// directory to build.
// directory to build. Only do this for packages, not files.
chdir := ""
if packagePath[0] == '_' {
if packagePath[0] == '_' && filepath.Ext(packagePath) != ".go" {
chdir = packagePath[1:]
packagePath = ""
}
Expand All @@ -69,17 +71,33 @@ func GoCrossCompile(packagePath string, platform Platform, outputTpl string, ldf
// GoMainDirs returns the file paths to the packages that are "main"
// packages, from the list of packages given. The list of packages can
// include relative paths, the special "..." Go keyword, etc.
func GoMainDirs(packages []string) ([]string, error) {
args := make([]string, 0, len(packages)+3)
args = append(args, "list", "-f", "{{.Name}}|{{.ImportPath}}")
args = append(args, packages...)
func GoMainDirs(compileThese []string) ([]string, error) {
// Separate the packages from the files
files := make([]string, 0)
packages := make([]string, 0)
for _, p := range compileThese {
if filepath.Ext(p) == ".go" {
files = append(files, p)
} else {
packages = append(packages, p)
}
}

output, err := execGo(nil, "", args...)
if err != nil {
return nil, err
results := make([]string, 0)
output := ""

if len(packages) != 0 {
args := make([]string, 0, len(packages)+3)
args = append(args, "list", "-f", "{{.Name}}|{{.ImportPath}}")
args = append(args, packages...)

var err error
output, err = execGo(nil, "", args...)
if err != nil {
return nil, err
}
}

results := make([]string, 0, len(output))
for _, line := range strings.Split(output, "\n") {
if line == "" {
continue
Expand All @@ -96,6 +114,8 @@ func GoMainDirs(packages []string) ([]string, error) {
}
}

results = append(results, files...)

return results, nil
}

Expand Down