Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
install.go: add ruby dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
SeekingMeaning committed Aug 2, 2020
1 parent 352cfa6 commit bed516e
Showing 1 changed file with 76 additions and 4 deletions.
80 changes: 76 additions & 4 deletions cmd/gofish/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"strings"
"time"

Expand All @@ -25,6 +24,76 @@ const installDesc = `
Install fish food.
`

const rubyDsl = `
def food(&block)
Food.new(&block)
end
class Food
def initialize(&block)
instance_eval(&block)
end
def name(val = nil); val.nil? ? @name : @name = val; end
def rig; '@RIG@'; end
def description(val = nil); val.nil? ? @description : @description = val; end
def license(val = nil); val.nil? ? @license : @license = val; end
def homepage(val = nil); val.nil? ? @homepage : @homepage = val; end
def caveats(val = nil); val.nil? ? @caveats : @caveats = val; end
def version(val = nil); val.nil? ? @version : @version = val; end
def packages; @packages ||= []; end
def package(&block); packages << Package.new(self, &block); end
def preinstallscript(val = nil); val.nil? ? @preinstallscript : @preinstallscript = val; end
def postinstallscript(val = nil); val.nil? ? @postinstallscript : @postinstallscript = val; end
end
class Package
def initialize(food, &block)
@food = food
instance_eval(&block)
end
def name; @food.name; end
def description; @food.description; end
def license; @food.license; end
def homepage; @food.homepage; end
def version; @food.version; end
def os(val = nil); val.nil? ? @os : @os = val; end
def arch(val = nil); val.nil? ? @arch : @arch = val; end
def url(val = nil); val.nil? ? @url : @url = val; end
def sha256(val = nil); val.nil? ? @sha256 : @sha256 = val; end
def resources; @resources ||= []; end
def resource(&block); resources << Resource.new(@food, self, &block); end
def mirrors; @mirrors ||= []; end
def mirror(val); @mirrors << mirror; end
end
class Resource
def initialize(food, package, &block)
@food = food
@package = package
instance_eval(&block)
end
def name; @food.name; end
def description; @food.description; end
def license; @food.license; end
def homepage; @food.homepage; end
def version; @food.version; end
def os; @package.os; end
def arch; @package.arch; end
def url; @package.url; end
def sha256; @package.sha256; end
def path(val = nil); val.nil? ? @path : @path = val; end
def installpath(val = nil); val.nil? ? @installpath : @installpath = val; end
def executable(val = nil); val.nil? ? @executable : @executable = val; end
end
`

func newInstallCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "install <food...>",
Expand Down Expand Up @@ -118,13 +187,16 @@ func getFood(foodName string) (*gofish.Food, error) {
}
mrb := ruby.NewMrb()
defer mrb.Close()
if _, err := mrb.LoadString(string(foodBytes)); err != nil {
if _, err := mrb.LoadString(strings.ReplaceAll(rubyDsl, "@RIG@", rig)); err != nil {
return nil, err
}
value, err := mrb.LoadString(string(foodBytes))
if err != nil {
return nil, err
}
var food gofish.Food
if err := ruby.Decode(&food, mrb.GetGlobalVariable(fmt.Sprintf("$%s", strings.ToLower(reflect.TypeOf(food).Name())))); err != nil {
if err := ruby.Decode(&food, value); err != nil {
return nil, err
}
food.Rig = rig
return &food, nil
}

0 comments on commit bed516e

Please sign in to comment.