Module: Atk

Defined in:
lib/atk/atk_info.rb,
lib/atk/autocomplete.rb,
lib/atk/commands/project.rb

Constant Summary collapse

@@atk_settings_key =
"atk_settings"

Class Method Summary collapse

Class Method Details

.autocomplete(which_command) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/atk/autocomplete.rb', line 2

def self.autocomplete(which_command)
    if which_command == '_'
        require_relative './yaml_info_parser.rb'
        begin
            puts Info.project_commands().keys.map { |each| each.gsub(' ', '\ ') }.join(' ')
        rescue => exception
            puts ""
        end
    end
end

.infoObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/atk/atk_info.rb', line 66

def self.info
    settings_path = Atk.paths[:info]
    # if it doesn't exist then create it
    if not FS.exist?(settings_path)
        FS.write("#{@@atk_settings_key}: {}", to: settings_path)
        return {}
    else
        data = YAML.load_file(settings_path)
        if data.is_a?(Hash)
            if data[@@atk_settings_key].is_a?(Hash)
                return data[@@atk_settings_key]
            end
        end
    end
    return {}
end

.not_yet_implementedObject



136
137
138
# File 'lib/atk/atk_info.rb', line 136

def self.not_yet_implemented()
    puts "Sorry, this feature is still under development"
end

.pathsObject



54
55
56
# File 'lib/atk/atk_info.rb', line 54

def self.paths
    return AtkPaths
end

.project(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
161
162
163
164
165
# File 'lib/atk/commands/project.rb', line 4

def self.project(args)
    # TODO: check to make sure project exists
    if args.length == 0
        puts "if you don't know how to use #{"project".blue} just run #{"project help".blue}"
        puts ""
        # if there are commands then show them
        begin
            commands = Info.project_commands
            if commands.is_a?(Hash) && commands.keys.size > 0
                puts "commands for current project:"
                puts `project commands`
            end
        rescue => exception
        end
    else
        # 
        # Check dependencies
        # 
            # if they're not met, then warn the user about that
            # check a hash of the file to see if anything has changed
        case args[0]
            when 'help', '--help', '-h'
                puts "                    \#{\"help\".yellow}\n                        \#{\"info:\".green} displays the avalible tools\n                        \#{\"examples:\".green} \#{'project help'.blue}\n                    \n                    \#{\"initialize\".yellow}\n                        \#{\"examples:\".green}\n                            \#{'project init'.blue}\n                            \#{'project initialize'.blue}\n                        \#{\"info:\".green}\n                            This will create an info.yaml in your current directory\n                            The info.yaml will contain all the standard project managment tools\n                            In the future this command will be more interactive\n\n                    \#{\"synchronize\".yellow}\n                        \#{\"examples:\".green}\n                            \#{'project sync'.blue}\n                            \#{'project synchronize'.blue}\n                            \#{'project synchronize --message=\\'updated the readme\\''.blue}\n                        \#{\"info:\".green}\n                            Adds, commits, and then pulls/pushes all git changes\n                            If there is merge conflict, it will show up as normal\n                        \#{\"format:\".green}\n                            \#{\"project\".blue} \#{\"synchronize\".yellow} \#{\"<package>\".cyan} \#{\"--message='your message'\".light_magenta}\n                    \n                    \#{\"execute\".yellow} \n                        \#{\"examples:\".green}\n                            \#{'project execute compile'.blue}\n                            \#{'project exec compile'.blue}\n                            \#{'project exec main'.blue}\n                            \#{'project exec server'.blue}\n                        \#{\"info:\".green}\n                            This will look at the info.yaml file in your project to find commands\n                            You can use the `project init` command to generate an info.yaml which \n                            has example commands. Commands can be CMD/terminal/console commands, or ruby code.\n                        \#{\"format:\".green}\n                            \#{\"project\".blue} \#{\"execute\".yellow} \#{\"<name-of-command>\".cyan} \#{\"<arg1-for-command>\".light_magenta} \#{\"<arg2-for-command>\".light_magenta} \#{\"<...etc>\".light_magenta}\n\n                    \#{\"commands\".yellow}\n                        \#{\"examples:\".green} \#{'project commands'.blue}\n                        \#{\"info:\".green}\n                            This will read the local info.yaml of your project to find commands\n                            then it will list out each command with a short preview of the contents of that command\n                HEREDOC\n            when 'initialize', 'init'\n                Info.init\n            when 'synchronize', 'sync'\n                # if there is an argument\n                git_folder_path = FS.dirname(Info.source_path)/\".git\"\n                if not FS.is_folder(git_folder_path)\n                    raise <<-HEREDOC.remove_indent\n                        \n                        \n                        The `project sync` command was called inside of \#{FS.dirname(Info.source_path)}\n                        However, there doesn't seem to be a git repository in this folder\n                        (and changes can't be saved/synced without a git repository)\n                    HEREDOC\n                end\n                message = args[1]\n                if message == nil\n                    message = \"\"\n                else\n                    if not message.start_with?('--message=')\n                        raise \"\\n\\nWhen giving arguments to the sync command, please give your message as:\\n\\n    project sync --message='whatever you wanted to say'\"\n                    else\n                        # remove the begining of the message\n                        message = args[1].sub(/^--message=/,\"\")\n                        # remove leading/trailing whitespace\n                        message.strip!\n                    end\n                end\n                if message.size == 0\n                    message = '-'\n                end\n                \n                # add everything\n                system('git add -A')\n                # commit everything\n                system('git', 'commit', '-m', message)\n                # pull down everything\n                system('git pull --no-edit')\n                # push up everything\n                system('git push')\n                \n            when 'mix'\n                not_yet_implemented()\n                structure_name = args[1]\n                # use this to mix a structure into the project\n                # TODO:\n                # get the context\n                    # if there is a --context='something' command line option, then use that\n                    # otherwise use the default(--context) speficied in the info.yaml\n                    # re-iterate through the info.yaml (advanced_setup) keys\n                    # find all of the \"when(--context = 'something')\" keys\n                    # find the (dependencies) sub-key for them, create one if the key doesn't exist\n                    # add the project and version to the \n            when 'add'\n                not_yet_implemented()\n                package = args[1]\n                # check if there is an info.yaml\n                # check if there is an local_package_manager in the info.yaml\n                # if there is only 1, then use it\n                # if there is more than one, ask which one the user wants to use\n            when 'remove'\n                not_yet_implemented()\n                package = args[1]\n                # check if there is an local_package_manager in the info.yaml\n                # if it does use it to remove the package\n            when 'execute', 'exec'\n                # extract the (project_commands) section from the info.yaml, \n                # then find the command with the same name as args[1] and run it\n                # TODO: use https://github.com/piotrmurach/tty-markdown#ttymarkdown- to highlight the ruby code \n                _, command_name, *command_args = args\n                command = Info.project_commands[command_name]\n                # temporairly set the dir to be the same as the info.yaml \n                FS.in_dir(Info.folder()) do\n                    if command.is_a?(String)\n                        -(command+' '+command_args.join(' '))\n                    elsif command.is_a?(Code)\n                        command.run(*command_args)\n                    elsif command == nil\n                        puts \"I don't think that command is in the info.yaml file\"\n                    end\n                end\n            when 'commands'\n                max_number_of_chars_to_show = 80\n                commands = Info.project_commands\n                if commands.keys.size == 0\n                    puts \"0 avalible commands\".cyan\n                else\n                    for each_key, each_value in commands\n                        puts \"    \#{each_key.to_s.yellow}: \#{each_value.to_s.strip[0..max_number_of_chars_to_show].sub(/(.*)[\\s\\S]*/,'\\1')}\"\n                    end\n                end\n            else\n                puts \"I don't recognized that command\\nhere's the `project --help` which might get you what you're looking for:\"\n                Atk.project([\"help\"])\n        end\n    end\nend\n".remove_indent

.run(package_name, arguments = []) ⇒ Object



131
132
133
134
# File 'lib/atk/atk_info.rb', line 131

def self.run(package_name, arguments=[])
    the_package = AtkPackage.new(package_name)
    the_package.run(arguments)
end

.save_info(new_hash) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/atk/atk_info.rb', line 83

def self.save_info(new_hash)
    settings_path = Atk.paths[:info]
    current_settings = Atk.info
    updated_settings = current_settings.merge(new_hash)
    
    info_data = YAML.load_file(Atk.paths[:info])
    if info_file.is_a?(Hash)
        info_data[@@atk_settings_key] = updated_settings
    else
        info_data = { @@atk_settings_key => updated_settings }
    end
    
    FS.save(info_data, to: Atk.paths[:info], as: :yaml )
end

.setup(package_name, arguments) ⇒ Object



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
# File 'lib/atk/atk_info.rb', line 98

def self.setup(package_name, arguments)
    repo_url = AtkPackage.new(package_name).url
    project_folder = Atk.info["project_folder"]
    # if there's no project folder
    if not project_folder
        # then use the current folder
        project_folder = FS.pwd
        puts "Project will be downloaded to #{project_folder.to_s.yellow}"
        puts "(your current directory)"
        puts ""
    end
    project_name = Console.ask("What do you want to name the project?")
    project_path = project_folder/project_name
    Git.ensure_cloned_and_up_to_date(project_path, repo_url)
    FS.in_dir(project_path) do
        setup_command = Info.project_commands['(setup)']
        if setup_command.is_a?(Code) || setup_command.is_a?(String)
            puts "\n\nRunning (setup) command:\n".green
            sleep 1
            if setup_command.is_a?(Code)
                setup_command.run(arguments)
            else
                system(setup_command + Console.make_arguments_appendable(arguments))
            end
        end
        puts "\n\n\n\n============================================================"
        puts "Finished running setup for: #{project_path.green}"
        puts "This project has these commands avalible:"
        system "project commands"
        puts "\ndon't forget to do:\n#{"cd '#{project_path}'".blue}"
    end
end

.temp_path(filename) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/atk/atk_info.rb', line 58

def self.temp_path(filename)
    new_path = Atk.paths[:temp]/filename
    # make sure the path is empty
    FS.write("", to: new_path)
    FS.delete(new_path)
    return new_path
end

.versionObject



49
50
51
52
# File 'lib/atk/atk_info.rb', line 49

def self.version
    require_relative '../atk_toolbox/version.rb'
    return AtkToolbox::VERSION
end