Module: Atk

Defined in:
lib/atk/atk_info.rb,
lib/update_handler.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.commands().keys.map { |each| each.gsub(' ', '\ ') }.join(' ')
        rescue => exception
            puts ""
        end
    end
end

.infoObject



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

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

.migrate(old_version, new_version) ⇒ Object



2
3
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
# File 'lib/update_handler.rb', line 2

def self.migrate(old_version, new_version)
    require 'atk_toolbox'
    
    # helper function
    download_and_install_command = ->(command) do
        atk_command_download_path = Atk.temp_path("#{command}.rb")
        source = "https://raw.githubusercontent.com/aggie-tool-kit/atk-toolbox/master/custom_bin"
        FS.download("#{source}/#{command}" , to: atk_command_download_path)
        Console.set_command(command, FS.read(atk_command_download_path))
    end
    
    #
    # overwrite the commands
    # 
    download_and_install_command["atk"]
    download_and_install_command["project"]
    download_and_install_command["_"]

    # 
    # print success
    # 
    puts ""
    puts ""
    puts ""
    puts "=============================="
    puts "        ATK installed "
    puts "=============================="
end

.not_yet_implementedObject



134
135
136
# File 'lib/atk/atk_info.rb', line 134

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

.pathsObject



52
53
54
# File 'lib/atk/atk_info.rb', line 52

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
# 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
        commands = Info.commands
        if commands.is_a?(Hash) && commands.keys.size > 0
            puts "commands for current project:"
            puts `project commands`
        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 <<-HEREDOC.remove_indent
                    #{"help".yellow}
                        #{"info:".green} displays the avalible tools
                        #{"examples:".green} #{'project help'.blue}
                    
                    #{"initialize".yellow}
                        #{"examples:".green}
                            #{'project init'.blue}
                            #{'project initialize'.blue}
                        #{"info:".green}
                            This will create an info.yaml in your current directory
                            The info.yaml will contain all the standard project managment tools
                            In the future this command will be more interactive

                    #{"synchronize".yellow}
                        #{"examples:".green}
                            #{'project sync'.blue}
                            #{'project synchronize'.blue}
                            #{'project synchronize --message=\'updated the readme\''.blue}
                        #{"info:".green}
                            Adds, commits, and then pulls/pushes all git changes
                            If there is merge conflict, it will show up as normal
                        #{"format:".green}
                            #{"project".blue} #{"synchronize".yellow} #{"<package>".cyan} #{"--message='your message'".light_magenta}
                    
                    #{"execute".yellow} 
                        #{"examples:".green}
                            #{'project execute compile'.blue}
                            #{'project exec compile'.blue}
                            #{'project exec main'.blue}
                            #{'project exec server'.blue}
                        #{"info:".green}
                            This will look at the info.yaml file in your project to find commands
                            You can use the `project init` command to generate an info.yaml which 
                            has example commands. Commands can be CMD/terminal/console commands, or ruby code.
                        #{"format:".green}
                            #{"project".blue} #{"execute".yellow} #{"<name-of-command>".cyan} #{"<arg1-for-command>".light_magenta} #{"<arg2-for-command>".light_magenta} #{"<...etc>".light_magenta}

                    #{"commands".yellow}
                        #{"examples:".green} #{'project commands'.blue}
                        #{"info:".green}
                            This will read the local info.yaml of your project to find commands
                            then it will list out each command with a short preview of the contents of that command
                HEREDOC
            when 'initialize', 'init'
                Info.init
            when 'synchronize', 'sync'
                # if there is an argument
                git_folder_path = FS.dirname(Info.path)/".git"
                if not FS.is_folder(git_folder_path)
                    raise <<-HEREDOC.remove_indent
                        
                        
                        The `project sync` command was called inside of #{FS.dirname(Info.path)}
                        However, there doesn't seem to be a git repository in this folder
                        (and changes can't be saved/synced without a git repository)
                    HEREDOC
                end
                message = args[1]
                if message == nil
                    message = ""
                else
                    if not message.start_with?('--message=')
                        raise "\n\nWhen giving arguments to the sync command, please give your message as:\n\n    project sync --message='whatever you wanted to say'"
                    else
                        # remove the begining of the message
                        message = args[1].sub(/^--message=/,"")
                        # remove leading/trailing whitespace
                        message.strip!
                    end
                end
                if message.size == 0
                    message = '-'
                end
                
                # add everything
                system('git add -A')
                # commit everything
                system('git', 'commit', '-m', message)
                # pull down everything
                system('git pull --no-edit')
                # push up everything
                system('git push')
                
            when 'mix'
                not_yet_implemented()
                structure_name = args[1]
                # use this to mix a structure into the project
                # TODO:
                # get the context
                    # if there is a --context='something' command line option, then use that
                    # otherwise use the default(--context) speficied in the info.yaml
                    # re-iterate through the info.yaml (advanced_setup) keys
                    # find all of the "when(--context = 'something')" keys
                    # find the (dependencies) sub-key for them, create one if the key doesn't exist
                    # add the project and version to the 
            when 'add'
                not_yet_implemented()
                package = args[1]
                # check if there is an info.yaml
                # check if there is an local_package_manager in the info.yaml
                # if there is only 1, then use it
                # if there is more than one, ask which one the user wants to use
            when 'remove'
                not_yet_implemented()
                package = args[1]
                # check if there is an local_package_manager in the info.yaml
                # if it does use it to remove the package
            when 'execute', 'exec'
                # extract the (project_commands) section from the info.yaml, 
                # then find the command with the same name as args[1] and run it
                # TODO: use https://github.com/piotrmurach/tty-markdown#ttymarkdown- to highlight the ruby code 
                _, command_name, *command_args = args
                command = Info.commands[command_name]
                # temporairly set the dir to be the same as the info.yaml 
                FS.in_dir(Info.folder()) do
                    if command.is_a?(String)
                        -(command+' '+command_args.join(' '))
                    elsif command.is_a?(Code)
                        command.run(*command_args)
                    elsif command == nil
                        puts "I don't think that command is in the info.yaml file"
                    end
                end
            when 'commands'
                max_number_of_chars_to_show = 80
                commands = Info.commands
                if commands.keys.size == 0
                    puts "0 avalible commands".cyan
                else
                    for each_key, each_value in commands
                        puts "    #{each_key.to_s.yellow}: #{each_value.to_s.strip[0..max_number_of_chars_to_show].sub(/(.*)[\s\S]*/,'\1')}"
                    end
                end
            else
                puts "I don't recognized that command\nhere's the `project --help` which might get you what you're looking for:"
                Atk.project(["help"])
        end
    end
end

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



129
130
131
132
# File 'lib/atk/atk_info.rb', line 129

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

.save_info(new_hash) ⇒ Object



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

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



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

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.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



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

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

.updateObject



138
139
140
# File 'lib/atk/atk_info.rb', line 138

def self.update()
    system(Atk.paths['gem'], 'install', "atk_toolbox")
end

.versionObject



46
47
48
49
50
# File 'lib/atk/atk_info.rb', line 46

def self.version
    require_relative '../atk_toolbox/version.rb'
    require_relative './version.rb'
    return Version.new(AtkToolbox::VERSION)
end