-
-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 'platforms' in both task and command (#980)
- Loading branch information
1 parent
63c50d1
commit aa6c7e4
Showing
10 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package taskfile | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Platform represents GOOS and GOARCH values | ||
type Platform struct { | ||
OS string | ||
Arch string | ||
} | ||
|
||
// ParsePlatform takes a string representing an OS/Arch combination (or either on their own) | ||
// and parses it into the Platform struct. It returns an error if the input string is invalid. | ||
// Valid combinations for input: OS, Arch, OS/Arch | ||
func (p *Platform) ParsePlatform(input string) error { | ||
// tidy up input | ||
platformString := strings.ToLower(strings.TrimSpace(input)) | ||
splitValues := strings.Split(platformString, "/") | ||
if len(splitValues) > 2 { | ||
return fmt.Errorf("task: Invalid OS/Arch provided: %s", input) | ||
} | ||
err := p.parseOsOrArch(splitValues[0]) | ||
if err != nil { | ||
return err | ||
} | ||
if len(splitValues) == 2 { | ||
return p.parseArch(splitValues[1]) | ||
} | ||
return nil | ||
} | ||
|
||
// supportedOSes is a list of supported OSes | ||
var supportedOSes = map[string]struct{}{ | ||
"windows": {}, | ||
"darwin": {}, | ||
"linux": {}, | ||
"freebsd": {}, | ||
} | ||
|
||
func isSupportedOS(input string) bool { | ||
_, exists := supportedOSes[input] | ||
return exists | ||
} | ||
|
||
// supportedArchs is a list of supported architectures | ||
var supportedArchs = map[string]struct{}{ | ||
"amd64": {}, | ||
"arm64": {}, | ||
"386": {}, | ||
} | ||
|
||
func isSupportedArch(input string) bool { | ||
_, exists := supportedArchs[input] | ||
return exists | ||
} | ||
|
||
// MatchesCurrentPlatform returns true if the platform matches the current platform | ||
func (p *Platform) MatchesCurrentPlatform() bool { | ||
return (p.OS == "" || p.OS == runtime.GOOS) && | ||
(p.Arch == "" || p.Arch == runtime.GOARCH) | ||
} | ||
|
||
// UnmarshalYAML implements yaml.Unmarshaler interface. | ||
func (p *Platform) UnmarshalYAML(node *yaml.Node) error { | ||
switch node.Kind { | ||
|
||
case yaml.ScalarNode: | ||
var platform string | ||
if err := node.Decode(&platform); err != nil { | ||
return err | ||
} | ||
if err := p.ParsePlatform(platform); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into platform", node.Line, node.ShortTag()) | ||
} | ||
|
||
// parseOsOrArch will check if the given input is a valid OS or Arch value. | ||
// If so, it will store it. If not, an error is returned | ||
func (p *Platform) parseOsOrArch(osOrArch string) error { | ||
if osOrArch == "" { | ||
return fmt.Errorf("task: Blank OS/Arch value provided") | ||
} | ||
if isSupportedOS(osOrArch) { | ||
p.OS = osOrArch | ||
return nil | ||
} | ||
if isSupportedArch(osOrArch) { | ||
p.Arch = osOrArch | ||
return nil | ||
} | ||
return fmt.Errorf("task: Invalid OS/Arch value provided (%s)", osOrArch) | ||
} | ||
func (p *Platform) parseArch(arch string) error { | ||
if arch == "" { | ||
return fmt.Errorf("task: Blank Arch value provided") | ||
} | ||
if p.Arch != "" { | ||
return fmt.Errorf("task: Multiple Arch values provided") | ||
} | ||
if isSupportedArch(arch) { | ||
p.Arch = arch | ||
return nil | ||
} | ||
return fmt.Errorf("task: Invalid Arch value provided (%s)", arch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
version: '3' | ||
|
||
tasks: | ||
build-windows: | ||
platforms: [windows] | ||
cmds: | ||
- echo 'Running task on windows' | ||
|
||
build-darwin: | ||
platforms: [darwin] | ||
cmds: | ||
- echo 'Running task on darwin' | ||
|
||
build-linux: | ||
platforms: [linux] | ||
cmds: | ||
- echo 'Running task on linux' | ||
|
||
build-freebsd: | ||
platforms: [freebsd] | ||
cmds: | ||
- echo 'Running task on freebsd' | ||
|
||
build-blank-os: | ||
platforms: [] | ||
cmds: | ||
- echo 'Running command' | ||
|
||
build-multiple: | ||
platforms: [] | ||
cmds: | ||
- cmd: echo 'Running command' | ||
- cmd: echo 'Running on Windows' | ||
platforms: [windows] | ||
- cmd: echo 'Running on Darwin' | ||
platforms: [darwin] | ||
|
||
build-amd64: | ||
platforms: [amd64] | ||
cmds: | ||
- echo "Running command on amd64" | ||
|
||
build-arm64: | ||
platforms: [arm64] | ||
cmds: | ||
- echo "Running command on arm64" | ||
|
||
build-mixed: | ||
cmds: | ||
- cmd: echo 'building on windows/arm64' | ||
platforms: [windows/arm64] | ||
- cmd: echo 'building on linux/amd64' | ||
platforms: [linux/amd64] | ||
- cmd: echo 'building on darwin' | ||
platforms: [darwin] |
Oops, something went wrong.