Class: Jumpstarter::InstructionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jumpstarter_core/parser.rb

Class Method Summary collapse

Class Method Details

.get_args(line) ⇒ Object

Gets the arguments of a line –foo = variable/boolean/number –“foo” = String –“foo bar baz” = String



14
15
16
# File 'lib/jumpstarter_core/parser.rb', line 14

def get_args(line)
    return Hash[line.scan(/--?([^=\s]+)(?:=(\"[^\"]+\"|\S+))?/)]
end

.parse(line) ⇒ Object

Parse each line individually If the command is recognized, create the corrisponding instruction class and call compose! on it

compose! returns a string that is valid ruby code and can such be run



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/jumpstarter_core/parser.rb', line 24

def parse(line)

    inst_elm = line.split

    ## Read the command
    cmd = inst_elm[0]

    ## Parse the arguments
    args = InstructionParser.get_args(line)

    ## Arguments that apply to all commands
    msg_success = args["msg_success"]
    msg_error = args["msg_error"]
    should_crash = args["should_crash"]

    # Switch over the command finding the appropiate
    # instruction. Once found, process the remaining
    # inst_elm or args and feed them into the class
    case cmd
    when "pip"
        package = inst_elm[1]
        return Jumpstarter::Pip::Install.new(
            package: package, 
            msg_success: msg_success, 
            msg_error: msg_error, 
            should_crash: should_crash
        ).compose!
    when "git"
        subcmd = inst_elm[1]
        remote = inst_elm[2] 
        branch = inst_elm[3] if inst_elm[3]
        case subcmd
        when "fork"
            return Jumpstarter::Git::Fork.new(
                remote: remote, 
                username: args["username"], 
                password: args["password"], 
                msg_success: msg_success, 
                msg_error: msg_error, 
                should_crash: should_crash
            ).compose!
        when "pull"
            return Jumpstarter::Git::Pull.new(
                remote: remote, 
                branch: branch, 
                msg_success: msg_success, 
                msg_error: msg_error, 
                should_crash: should_crash
            ).compose!
        when "clone"
            return Jumpstarter::Git::Clone.new(
                remote: remote, 
                username: args["username"], 
                msg_success: msg_success, 
                msg_error: msg_error, 
                should_crash: should_crash
            ).compose!
        else
        end
    when "bash"
        return Jumpstarter::Bash::Run.new(
            cmd: args["cmd"], 
            msg_success: msg_success, 
            msg_error: msg_error, 
            should_crash: should_crash
        ).compose!
    when "cd"
        path = inst_elm[1]
        return Jumpstarter::Bash::CD.new(
            path: path, 
            msg_success: msg_success, 
            msg_error: msg_error, 
            should_crash: should_crash
        ).compose!
    when "xcode"
        subcmd = inst_elm[1]
        case subcmd
        when "scheme"
            action = args["action"]
            case action
            when "edit"
                return Jumpstarter::Xcode::EditSchemeEnvVars.new(
                        proj_path: args["path"],
                        scheme_name: args["scheme_name"], 
                        shared: args["shared"],
                        key: args["env_var_key"],
                        value: args["env_var_val"],
                        msg_success: msg_success,
                        msg_error: msg_error,
                        should_crash: should_crash
                ).compose!
            when "duplicate"
                return Jumpstarter::Xcode::DuplicateScheme.new(
                        proj_path: args["path"],
                        original_scheme_name: args["original_scheme_name"], 
                        new_scheme_name: args["new_scheme_name"], 
                        original_shared: args["original_shared"],
                        new_shared: args["new_shared"],
                        msg_success: msg_success,
                        msg_error: msg_error,
                        should_crash: should_crash
                ).compose!
            when "create"
                return Jumpstarter::Xcode::CreateScheme.new(
                        proj_path: args["path"],
                        scheme_name: args["scheme_name"], 
                        shared: args["shared"],
                        msg_success: msg_success,
                        msg_error: msg_error,
                        should_crash: should_crash
                ).compose!
            else
            end
        when "target"
            action = args["action"]
            case action
            when "edit"
                return Jumpstarter::Xcode::EditTargetBundleID.new(
                        proj_path: args["path"],
                        bundle_id: args["bundle_id"],
                        team_id: args["team_id"],
                        code_siging_identity: args["code_siging_identity"],
                        msg_success: msg_success,
                        msg_error: msg_error,
                        should_crash: should_crash
                ).compose!
            else
            end
        else
        end
    else
        # In a case where the command is not understood
        # assume the line is a ruby line and simply return
        # that line to the starter.rb file to run normally
        return "#{line}"
    end
end