README
¶
go-cask
NOTICE: Currently in development.
This library provides functionality for working with Homebrew-Cask casks.
What "cask" means?
The "cask" is a small Ruby block in a separate file that is used to describe the application in Homebrew-Cask project. You can learn more about them by reading through the Homebrew-Cask "Synopsis".
Why is this library needed and what it does?
This library attempts to provide a way of parsing and extracting basic
information from casks for later use in Go. It parses the cask Ruby block and
creates the corresponding cask.Cask
struct.
Features
- Conditional statements
- MacOS.version
- Language blocks
- String interpolations
-
#{version}
-
#{language}
-
Supported stanzas
Below you can find a list of all supported cask stanzas that this library can understand and recognize during the parsing phase. If the checkbox next to the stanza is not ticked, that stanza is not supported yet.
To learn more about all available cask stanzas check out the Homebrew-Cask "All stanzas".
Required
-
version
-
sha256
-
url
-
name
-
homepage
Artifacts
-
app
-
target:
-
-
pkg
-
allow_untrusted:
-
-
binary
-
target:
-
-
colorpicker
-
dictionary
-
font
-
input_method
-
internet_plugin
-
prefpane
-
qlplugin
-
screen_saver
-
service
-
audio_unit_plugin
-
vst_plugin
-
vst3_plugin
-
suite
-
artifact
-
installer
-
stage_only
Optional
-
uninstall
-
zap
-
appcast
-
depends_on
-
conflicts_with
-
caveats
- indented heredoc (
<<-EOS
) - "squiggly" heredoc (
<<~EOS
)
- indented heredoc (
-
preflight
-
postflight
-
uninstall_preflight
-
uninstall_postflight
-
language
-
accessibility_access
-
container nested:
-
container type:
-
gpg
-
auto_updates
Examples
First example
For the first example we will parse the example-one.rb cask from our testdata directory.
package main
import (
"fmt"
"github.com/victorpopkov/go-cask"
)
func main() {
// for this example we will load the cask from our testdata directory
content := string(getTestdata("example-one.rb"))
// example
c := cask.NewCask(content)
err := c.Parse()
if err == nil {
fmt.Println("Token:", c.Token)
for i, v := range c.Variants {
fmt.Printf("Variant #%d:\n", i+1)
fmt.Printf("%10s: %s\n", "version", v.GetVersion())
fmt.Printf("%10s: %s\n", "sha256", v.GetSHA256())
fmt.Printf("%10s: %s\n", "url", v.GetURL())
fmt.Printf("%10s: %s\n", "appcast", v.GetAppcast().URL)
fmt.Printf("%10s: %v\n", "names", v.GetNames())
fmt.Printf("%10s: %s\n", "homepage", v.GetHomepage())
// artifacts
fmt.Printf("%10s: ", "artifacts")
if len(v.GetArtifacts()) > 0 {
for i, a := range v.GetArtifacts() {
if i == 0 {
fmt.Printf("%s\n", a.String())
} else {
fmt.Printf("%12s%s\n", "", a.String())
}
}
} else {
fmt.Printf("%v\n", v.GetArtifacts())
}
// macOS
fmt.Printf("%10s: %s [minimum]\n", "macOS", v.MinimumSupportedMacOS)
fmt.Printf("%12s%s [maximum]\n", "", v.MaximumSupportedMacOS)
}
}
// Output:
// Token: example-one
// Variant #1:
// version: 2.0.0
// sha256: f22abd6773ab232869321ad4b1e47ac0c908febf4f3a2bd10c8066140f741261
// url: https://example.com/app_2.0.0.dmg
// appcast: https://example.com/sparkle/2/appcast.xml
// names: [Example Example One]
// homepage: https://example.com/
// artifacts: app, Example 2.0.app => Example.app
// app, Example 2.0 Uninstaller.app
// binary, #{appdir}/Example 2.0.app/Contents/MacOS/example-one => example
// macOS: macOS High Sierra (10.13) [minimum]
// macOS High Sierra (10.13) [maximum]
}
Second example
For the second example we will parse the example-two.rb cask from our testdata directory.
package main
import (
"fmt"
"github.com/victorpopkov/go-cask"
)
func main() {
// for this example we will load the cask from our testdata directory
content := string(getTestdata("example-two.rb"))
// example
c := cask.NewCask(content)
err := c.Parse()
if err == nil {
fmt.Println("Token:", c.Token)
for i, v := range c.Variants {
fmt.Printf("Variant #%d:\n", i+1)
fmt.Printf("%10s: %s\n", "version", v.GetVersion())
fmt.Printf("%10s: %s\n", "sha256", v.GetSHA256())
fmt.Printf("%10s: %s\n", "url", v.GetURL())
fmt.Printf("%10s: %s\n", "appcast", v.GetAppcast().URL)
fmt.Printf("%10s: %v\n", "names", v.GetNames())
fmt.Printf("%10s: %s\n", "homepage", v.GetHomepage())
// artifacts
fmt.Printf("%10s: ", "artifacts")
if len(v.GetArtifacts()) > 0 {
for i, a := range v.GetArtifacts() {
if i == 0 {
fmt.Printf("%s\n", a.String())
} else {
fmt.Printf("%12s%s\n", "", a.String())
}
}
} else {
fmt.Printf("%v\n", v.GetArtifacts())
}
// macOS
fmt.Printf("%10s: %s [minimum]\n", "macOS", v.MinimumSupportedMacOS)
fmt.Printf("%12s%s [maximum]\n", "", v.MaximumSupportedMacOS)
}
}
// Output:
// Token: example-two
// Variant #1:
// version: 1.5.0
// sha256: 1f4dc096d58f7d21e3875671aee6f29b120ab84218fa47db2cb53bc9eb5b4dac
// url: https://example.com/app_1.5.0.pkg
// appcast: https://example.com/sparkle/1/el_capitan.xml
// names: [Example Example Two]
// homepage: https://example.com/
// artifacts: pkg, app_1.5.0.pkg, allow_untrusted: true
// macOS: Mac OS X Tiger (10.4) [minimum]
// OS X El Capitan (10.11) [maximum]
// Variant #2:
// version: 2.0.0
// sha256: f22abd6773ab232869321ad4b1e47ac0c908febf4f3a2bd10c8066140f741261
// url: https://example.com/app_2.0.0.pkg
// appcast: https://example.com/sparkle/2/appcast.xml
// names: [Example Example Two]
// homepage: https://example.com/
// artifacts: pkg, app_2.0.0.pkg, allow_untrusted: true
// macOS: macOS High Sierra (10.13) [minimum]
// macOS High Sierra (10.13) [maximum]
}
License
Released under the MIT License.
Documentation
¶
Overview ¶
Example (One) ¶
Output: Token: example-one Variant #1: version: 2.0.0 sha256: f22abd6773ab232869321ad4b1e47ac0c908febf4f3a2bd10c8066140f741261 url: https://example.com/app_2.0.0.dmg appcast: https://example.com/sparkle/2/appcast.xml names: [Example Example One] homepage: https://example.com/ artifacts: app, Example 2.0.app => Example.app app, Example 2.0 Uninstaller.app binary, #{appdir}/Example 2.0.app/Contents/MacOS/example-one => example macOS: macOS High Sierra (10.13) [minimum] macOS High Sierra (10.13) [maximum]
Example (Two) ¶
Output: Token: example-two Variant #1: version: 1.5.0 sha256: 1f4dc096d58f7d21e3875671aee6f29b120ab84218fa47db2cb53bc9eb5b4dac url: https://example.com/app_1.5.0.pkg appcast: https://example.com/sparkle/1/el_capitan.xml names: [Example Example Two] homepage: https://example.com/ artifacts: pkg, app_1.5.0.pkg, allow_untrusted: true macOS: Mac OS X Tiger (10.4) [minimum] OS X El Capitan (10.11) [maximum] Variant #2: version: 2.0.0 sha256: f22abd6773ab232869321ad4b1e47ac0c908febf4f3a2bd10c8066140f741261 url: https://example.com/app_2.0.0.pkg appcast: https://example.com/sparkle/2/appcast.xml names: [Example Example Two] homepage: https://example.com/ artifacts: pkg, app_2.0.0.pkg, allow_untrusted: true macOS: macOS High Sierra (10.13) [minimum] macOS High Sierra (10.13) [maximum]
Index ¶
- Variables
- type Appcast
- type Artifact
- type ArtifactType
- type BaseStanza
- type Cask
- type Errors
- type Homepage
- type Lexer
- type MacOS
- type Name
- type Parser
- type SHA256
- type Stanza
- type StateFn
- type Token
- type TokenType
- type URL
- type Variant
- func (v *Variant) AddArtifact(artifact *Artifact)
- func (v *Variant) AddName(name *Name)
- func (v *Variant) GetAppcast() (a Appcast)
- func (v *Variant) GetArtifacts() (a []Artifact)
- func (v *Variant) GetHomepage() (h Homepage)
- func (v *Variant) GetNames() (n []Name)
- func (v *Variant) GetSHA256() SHA256
- func (v *Variant) GetURL() (u URL)
- func (v *Variant) GetVersion() Version
- type Version
- func (v Version) AfterColon() (string, error)
- func (v Version) AfterComma() (string, error)
- func (v Version) BeforeColon() (string, error)
- func (v Version) BeforeComma() (string, error)
- func (v Version) DotsToHyphens() (string, error)
- func (v Version) DotsToUnderscores() (string, error)
- func (v Version) HasVersionStringInterpolation(str string) bool
- func (v Version) InterpolateIntoString(str string) (result string)
- func (v Version) Major() (string, error)
- func (v Version) MajorMinor() (string, error)
- func (v Version) MajorMinorPatch() (string, error)
- func (v Version) Minor() (string, error)
- func (v Version) NoDots() (string, error)
- func (v Version) Patch() (string, error)
- func (v Version) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var LexStartFn = startLexer
LexStartFn represents the entrypoint the Lexer uses to start processing the input.
Functions ¶
This section is empty.
Types ¶
type Appcast ¶
type Appcast struct { BaseStanza // URL specifies the appcast URL. URL string }
An Appcast represents an appcast cask stanza.
func NewAppcast ¶
NewAppcast creates a new Appcast instance and returns its pointer. Requires both Appcast.URL and Appcast.Checkpoint to be passed as arguments.
type Artifact ¶
type Artifact struct { // Type specifies the artifact type. Type ArtifactType // Value specifies the artifact value. Value string // Target specifies the "target:" value. By default, it's empty string. Target string // AllowUntrusted specifies the "allow_untrusted:" value. By default, it's // false. This should be true only if the Artifact.Type is ArtifactPkg. AllowUntrusted bool }
An Artifact represents the cask artifact stanza itself.
func NewArtifact ¶
func NewArtifact(t ArtifactType, value string) *Artifact
NewArtifact creates a new Artifact instance and returns its pointer. Requires both Artifact.Type and Artifact.Value to be passed as arguments.
type ArtifactType ¶
type ArtifactType int
An ArtifactType represents a known artifact stanza type.
const ( ArtifactApp ArtifactType = iota ArtifactPkg ArtifactBinary )
Different supported artifact types.
func (ArtifactType) String ¶
func (t ArtifactType) String() string
String returns the string representation of the ArtifactType.
type BaseStanza ¶
type BaseStanza struct { // IsGlobal specifies if the appcast belongs to all Cask.Variants. If the // stanza wasn't found inside if statement, the stanza should be considered as // global and this value should be true. By default, this value is "false". IsGlobal bool }
A BaseStanza represents a base for all stanzas. Shouldn't be used as is, but inherited by type specific stanzas.
type Cask ¶
type Cask struct { // Token specifies the cask token. Token string // Content specifies the string content of the loaded cask. Content string // Variants specifies all cask variants represented as a slice of Variant // pointers. Variants []*Variant // contains filtered or unexported fields }
A Cask represents the cask used in Homebrew-Cask.
func (*Cask) AddVariant ¶
AddVariant adds a new Variant pointer to the Cask.Variants slice.
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
Errors represents a group of errors and its context.
type Homepage ¶
type Homepage struct { BaseStanza // Value specifies the stanza value. Value string }
A Homepage represents a homepage cask stanza.
func NewHomepage ¶
NewHomepage creates a new Homepage instance and returns its pointer. Requires Homepage.Value to be passed as argument.
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer is the engine to process input and emit Tokens.
func NewLexer ¶
NewLexer creates a new Lexer instance and returns its pointer. Requires an input to be passed as an argument that is ready to be processed.
type MacOS ¶
type MacOS int
A MacOS represents the available macOS versions.
const ( MacOSHighSierra MacOS = iota MacOSSierra MacOSElCapitan MacOSYosemite MacOSMavericks MacOSMountainLion MacOSLion MacOSSnowLeopard MacOSLeopard MacOSTiger )
Different macOS releases.
type Name ¶
type Name struct { BaseStanza // Value specifies the stanza value. Value string }
A Name represents a name cask stanza.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
A Parser represents the parser that uses the emitted token provided by Lexer.
func NewParser ¶
NewParser creates a new Parser instance and returns its pointer. Requires a Lexer and a Cask to be specified as arguments.
func (*Parser) ParseArtifact ¶
ParseArtifact parses the supported artifact if the Parser.currentToken literal value matches the supported one. It runs the corresponding artifact specific parsing function. Returns an "artifact not found" error if the Parser.currentToken literal value doesn't match any supported one.
func (*Parser) ParseConditionMacOS ¶
ParseConditionMacOS parses the "MacOS.version" condition statement. Returns both the minimum and maximum macOS releases. By default, the minimum is MacOSTiger and the maximum matches the latest macOS release which is MacOSHighSierra.
type SHA256 ¶
type SHA256 struct { BaseStanza // Value specifies the stanza value. Value string }
A SHA256 represents a sha256 cask stanza.
type Stanza ¶
type Stanza interface {
String() string
}
A Stanza represents the interface that each stanza Type specific stanza should implement.
type StateFn ¶
StateFn represents a function which is capable of lexing parts of the input. It returns another StateFn to proceed with.
Typically a state function would get called from LexStartFn and should return LexStartFn to go back to the decision loop. It also could return another non start state function if the partial input to parse is abiguous.
type Token ¶
type Token struct { // Type specifies the recognized token type. Type TokenType // Literal specifies the literal representation of the token which is the // token value itself. Literal string // Position specifies the position where token is found. Position int }
A Token represents a known token used in Lexer with its literal representation.
type TokenType ¶
type TokenType int
TokenType represents a known token type.
const ( EOF TokenType = iota // end of input ILLEGAL // an illegal/unknown character CONST GLOBAL IDENT INT STRING SYMBOL // :symbol // %r{} PNREGEXP // %r PNSTART // left delimiter ('{' or other) PNEND // right delimiter ('}' or other) HEREDOC HEREDOCSTART HEREDOCEND ASSIGN // = ASTERISK // * BANG // ! MINUS // - PLUS // + SLASH // / MODULUS // % EQ // == GT // > LT // < NOTEQ // != COMMA // , NEWLINE // \n SEMICOLON // ; COLON // : DOT // . LBRACE // { LBRACKET // [ LPAREN // ( PIPE // | RBRACE // } RBRACKET // ] RPAREN // ) SCOPE // :: REGEXP CLASS DEF DO ELSE ELSEIF END FALSE IF MODULE NIL RETURN SELF THEN TRUE YIELD )
Different token types that can be recognized.
func LookupIdent ¶
LookupIdent returns a TokenType keyword if ident is in the keywords map. If specified ident starts with an upper character it will return a CONST TokenType. Otherwise, it returns IDENT.
type URL ¶
type URL struct { BaseStanza // Value specifies the stanza value. Value string }
An URL represents an url cask stanza.
type Variant ¶
type Variant struct { // Version specifies the version stanza. Version *Version // SHA256 specifies the SHA256 checksum for the downloaded Variant.URL file. SHA256 *SHA256 // URL specifies the url stanza. URL *URL // Appcast specifies the appcast stanza. Appcast *Appcast // Names specify the application names. Each cask can have multiple names. Names []*Name // Homepage specifies the application vendor homepage stanza. Homepage *Homepage // Artifacts specify artifact stanzas. Artifacts []*Artifact // MinimumSupportedMacOS specifies the minimum supported macOS release. By // default each cask uses the latest stable macOS release. MinimumSupportedMacOS MacOS // MaximumSupportedMacOS specifies the maximum supported macOS release. By // default each cask uses the latest stable macOS release. MaximumSupportedMacOS MacOS }
A Variant represents a single cask variant.
func (*Variant) AddArtifact ¶
AddArtifact adds a new Artifact pointer to the Variant.Artifacts slice.
func (*Variant) GetAppcast ¶
GetAppcast returns the Appcast struct from the existing Variant.Appcast struct pointer and interpolates the version into the Variant.Appcast.URL if available.
func (*Variant) GetArtifacts ¶
GetArtifacts returns the []Artifacts slice from the existing []Variant.Artifacts slice pointer and interpolates the version into each artifact value if available.
func (*Variant) GetHomepage ¶
GetHomepage returns the Homepage struct from the existing Variant.Homepage struct pointer and interpolates the version into the Variant.Homepage.Value if available.
func (*Variant) GetNames ¶
GetNames returns the []Name slice from the existing []Variant.Names slice pointer and interpolates the version into each name if available.
func (*Variant) GetSHA256 ¶
GetSHA256 returns the SHA256 struct from the existing Variant.SHA256 struct pointer.
func (*Variant) GetURL ¶
GetURL returns the URL struct from the existing Variant.URL struct pointer and interpolates the version into the Variant.URL.Value if available.
func (*Variant) GetVersion ¶
GetVersion returns the Version struct from the existing Variant.Version struct pointer.
type Version ¶
type Version struct { BaseStanza // Value specifies the stanza value. Value string }
A Version represents a version cask stanza.
func NewVersion ¶
NewVersion creates a new Version instance and returns its pointer. Requires Version.Value to be passed as argument.
func (Version) AfterColon ¶
AfterColon extracts the Version.Value part after colon and returns the result string.
func (Version) AfterComma ¶
AfterComma extracts the Version.Value part after comma and returns the result string.
func (Version) BeforeColon ¶
BeforeColon extracts the Version.Value part before colon and returns the result string.
func (Version) BeforeComma ¶
BeforeComma extracts the Version.Value part before comma and returns the result string.
func (Version) DotsToHyphens ¶
DotsToHyphens converts all Version.Value dots to hyphens and returns the result string.
func (Version) DotsToUnderscores ¶
DotsToUnderscores converts all Version.Value dots to underscores and returns the result string.
func (Version) HasVersionStringInterpolation ¶
HasVersionStringInterpolation check whether the provided string has a Ruby syntax version string interpolation.
func (Version) InterpolateIntoString ¶
InterpolateIntoString interpolates existing version into the provided string with Ruby interpolation syntax.
func (Version) Major ¶
Major extracts the major semantic version part from Version.Value and returns the result string.
func (Version) MajorMinor ¶
MajorMinor extracts the major and minor semantic version parts from Version.Value and returns the result string.
func (Version) MajorMinorPatch ¶
MajorMinorPatch extracts the major, minor and patch semantic version parts from Version.Value and returns the result string.
func (Version) Minor ¶
Minor extracts the minor semantic version part from Version.Value and returns the result string.