Class: PairProgrammer::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pairprogrammer/command.rb

Constant Summary collapse

@@default_commands =

default

{
    python: "python3",
}

Class Method Summary collapse

Class Method Details

.display_command(command, arguments) ⇒ Object



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
# File 'lib/pairprogrammer/command.rb', line 7

def self.display_command(command, arguments)
    case command
    when "yarn"
        "running `yarn #{arguments["command"]}`"
    when "mv"
        puts "moving #{arguments["source"]} to #{arguments["destination"]}"
    when "python"
        "running `python #{arguments["command"]}`"
    when "ls"
        "listing files and directories for #{arguments["directory_path"]}"
    when "bundle"
        puts "running `bundle #{arguments["command"]}`"
    when "rails"
        "running `rails #{arguments["command"]}`"
    when "comment"
        nil
    when "write_file"
        puts "updating #{arguments["file_path"]}"
    when "create_directory"
        puts "creating directory #{arguments["directory_path"]}"
    when "delete_file"
        puts "deleting #{arguments["file_path"]}"
    when "view_changes"
        # TODO
    when "delete_lines"
        puts "deleting lines #{line_numbers} from #{arguments["file_path"]}"
    when "rspec"
        puts "running `rspec #{arguments["file_path"]}`"
    when "ask_question"
        nil # will ask question
    when "read_file"
        puts "reading #{arguments["file_path"]}"
    when "update_file"
        puts "updating #{arguments["file_path"]}"
    when "create_file"
        puts "creating #{arguments["file_path"]}"
    end
end

.insert_content_at_line(file_path, content, line_number) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pairprogrammer/command.rb', line 145

def self.insert_content_at_line(file_path, content, line_number)
    # Read the file's content into an array
    file_content = File.readlines(file_path)

    # Insert the new content at the desired line number
    file_content.insert(line_number, content)

    # Write the modified content back to the file
    File.open(file_path, 'w') do |file|
    file_content.each { |line| file.puts(line) }
    end
end

.python_commandObject



51
52
53
# File 'lib/pairprogrammer/command.rb', line 51

def self.python_command
    PairProgrammer::Configuration.python_command || @@default_commands[:python]
end

.run(command, arguments) ⇒ Object



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
# File 'lib/pairprogrammer/command.rb', line 65

def self.run(command, arguments)
    case command
    when "update_file"
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        file_content = File.readlines(file_path)
        file_content.insert(arguments["line_number"], arguments["content"])
        File.open(file_path, 'w') do |file|
            file_content.each { |line| file.puts(line) }
        end
    when "yarn"
        run_shell "cd #{PairProgrammer::Configuration.root} && yarn #{arguments["command"]}"
    when "mv"
        run_shell "cd #{PairProgrammer::Configuration.root} && mv #{arguments["source"]} #{arguments["destination"]}"
    when "python"
        run_shell "cd #{PairProgrammer::Configuration.root} && #{python_command} manage.py #{arguments["command"]}"
    when "ls"
        run_shell "cd #{PairProgrammer::Configuration.root} && ls #{arguments["directory_path"]}"
    when "bundle"
        run_shell "cd #{PairProgrammer::Configuration.root} && bundle #{arguments["command"]}"
    when "rails"
        run_shell "cd #{PairProgrammer::Configuration.root} && rails #{arguments["command"]}"
    when "comment"
        "comment received"
    when "write_file"
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        File.open(file_path, "w") do |file|
            file.puts(arguments["content"])
        end
    when "create_directory"
        directory_path = PairProgrammer::Configuration.absolute_path(arguments["directory_path"])
        FileUtils.mkdir_p(directory_path)
    when "delete_file"
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        File.delete(file_path)
    when "view_changes"
        # TODO
    when "delete_lines"
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        line_numbers = arguments["line_numbers"]

        # Read the contents of the file into memory
        lines = File.readlines(file_path)

        # Delete the specified lines from the contents
        lines.delete_if.with_index { |line, index| line_numbers.include?(index) }

        # Write the modified contents back to the file
        File.write(file_path, lines.join)
    when "rspec"
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        run_shell "cd #{PairProgrammer::Configuration.root} && rspec #{file_path}}"
    when "ask_question"
        puts arguments["question"]
        STDIN.gets.chomp
    when "read_file"
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        if File.exists?(file_path)
            File.read(file_path)
        else
            "file does not exist"
        end
    when "create_file"
        # TODO return response if file is already created
        file_path = PairProgrammer::Configuration.absolute_path(arguments["file_path"])
        FileUtils.touch(file_path)
    else
        raise "Invalid command: #{command}"
    end
end

.run_shell(command) ⇒ Object

TODO if a process hangs, ie it asks for user input, need to kill it or let the user interact with it



136
137
138
139
140
141
142
143
144
# File 'lib/pairprogrammer/command.rb', line 136

def self.run_shell(command)
    output = ""
    Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
        while line = stdout_err.gets
            output += line
        end
    end
    output
end