Class: Sc2::Cli::Ladderzip

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sc2ai/cli/ladderzip.rb

Overview

Populating “./build” with our source…

  • Symlink executable ./build/bin/ladder to ./.build/botname for ladder

  • copy our source to .build, sans .ladderignore

copy .build to docker bundle install on docker zipping up code + ruby + gems get zip, clean up stop docker standard:disable Style/GlobalVars

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject



63
64
65
# File 'lib/sc2ai/cli/ladderzip.rb', line 63

def self.source_paths
  [Pathname(".").expand_path.to_s]
end

.source_rootObject



67
68
69
# File 'lib/sc2ai/cli/ladderzip.rb', line 67

def self.source_root
  Paths.template_root.to_s
end

Instance Method Details

#bot_validationObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sc2ai/cli/ladderzip.rb', line 45

def bot_validation
  if Pathname("./boot.rb").exist?
    say_status("detected boot.rb", "found", :green)
  else
    say_status("detected boot.rb", "not found", :red)
    raise Sc2::Error, "boot.rb not found. Bot started from wrong directory."
  end

  require "./boot"

  if $bot.is_a? Sc2::Player
    say_status("instance $bot", $bot.class, :green)
  else
    say_status("instance $bot", $bot.class, :red)
    raise Sc2::Error, "$bot instance nil or not Sc2::Player. Review boot.rb to ensure it creates $bot."
  end
end

#byeObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/sc2ai/cli/ladderzip.rb', line 160

def bye
  say ""
  say "Your bot is ready to upload. Use the file 'build.zip'."
  say ""
  say "A friendly reminder that since you passed \"#{botname}\", "
  say "aiarena expects your bot to be named exactly that."
  say "If the names mismatch, simply rerun this command with the correct BOTNAME."
  say ""
  say "Good luck."
end

#cleanObject



151
152
153
# File 'lib/sc2ai/cli/ladderzip.rb', line 151

def clean
  remove_file "./.build"
end

#compressObject



131
132
133
134
135
# File 'lib/sc2ai/cli/ladderzip.rb', line 131

def compress
  say_status("docker", "compress ladder zip...", :cyan)
  cmd = "docker compose -f #{@compose_file} exec --workdir /root/ruby-builder bot zip -qry9 /root/build.zip ."
  Kernel.system(cmd)
end

#copy_build_to_dockerObject



113
114
115
116
117
# File 'lib/sc2ai/cli/ladderzip.rb', line 113

def copy_build_to_docker
  say_status("docker", "copying source...", :cyan)
  cmd = "docker compose -f #{@compose_file} cp ./.build/. bot:/root/ruby-builder/"
  Kernel.system(cmd)
end

#create_executableObject



75
76
77
78
79
# File 'lib/sc2ai/cli/ladderzip.rb', line 75

def create_executable
  template("ladderzip/bin/ladder", "./.build/bin/ladder")
  # This fails on Windows. creating by hand in #link_ladder function below.
  # create_link("./.build/#{botname}", "./bin/ladder")
end

#docker_existsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sc2ai/cli/ladderzip.rb', line 20

def docker_exists
  if Kernel.system("docker --version", out: File::NULL, err: File::NULL)
    say_status("docker", "found", :green)
  else
    say_status("docker", "not found", :red)
    say("Please install 'docker' and/or ensure it's in your PATH to continue.")
    raise SystemExit
  end

  if Kernel.system("docker info", out: File::NULL, err: File::NULL)
    say_status("docker engine", "found", :green)
  else
    say("  ")
    say_status("docker engine", "offline", :red)
    say("Please start docker engine. If you have Docker Desktop installed, open it before retrying.")
    raise SystemExit
  end
end

#ensure_build_dirObject



71
72
73
# File 'lib/sc2ai/cli/ladderzip.rb', line 71

def ensure_build_dir
  empty_directory "./.build"
end

#install_gemsObject



125
126
127
128
129
# File 'lib/sc2ai/cli/ladderzip.rb', line 125

def install_gems
  say_status("docker", "bundle install...", :cyan)
  cmd = "docker compose -f #{@compose_file} exec --workdir /root/ruby-builder bot bundle install"
  Kernel.system(cmd)
end


119
120
121
122
123
# File 'lib/sc2ai/cli/ladderzip.rb', line 119

def link_ladder
  say_status("docker", "linking executable...", :cyan)
  cmd = "docker compose -f #{@compose_file} exec --workdir /root/ruby-builder bot ln -s ./bin/ladder ./#{botname}"
  Kernel.system(cmd)
end

#populate_buildObject



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
# File 'lib/sc2ai/cli/ladderzip.rb', line 86

def populate_build
  # Manually parse *ignore file. Not doing a docker build on this simply to abuse dockerignore.
  ignore_pattern = File.open("./.ladderignore").each_line
    .collect(&:strip)
    .reject { |pt| pt&.start_with?("#") || pt&.size&.zero? }
  ignore_pattern << "**.build/*"
  ignore_pattern << "**.ruby/*"

  include_pattern = ignore_pattern.select { |pt| pt&.start_with?("!") }
  ignore_pattern -= include_pattern

  include_pattern.collect! { |pt| pt.delete_prefix("!") }

  files = Dir.glob("**/*")

  ignored_files = files
    .select { |f| ignore_pattern.any? { |p| File.fnmatch?(p, f) } }
    .reject { |f| include_pattern.any? { |p| File.fnmatch?(p, f) } }

  files -= ignored_files unless ignored_files.nil?

  files.each do |file|
    next if File.directory?(file)
    copy_file file, "./.build/#{file}"
  end
end

#retreive_zipObject



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sc2ai/cli/ladderzip.rb', line 137

def retreive_zip
  say_status("docker", "pulling 'build.zip'", :green)
  cmd = "docker compose -f #{@compose_file} cp bot:/root/build.zip ."
  Kernel.system(cmd)
  if File.exist?("./build.zip")
    say_status("docker", "pulled 'build.zip' ok", :green)
  else
    say_status("docker", "error pulling 'build.zip'", :red)
    exit
  end
  cmd = "docker compose -f #{@compose_file} exec bot rm /root/build.zip"
  Kernel.system(cmd)
end

#set_compose_fileObject



39
40
41
42
43
# File 'lib/sc2ai/cli/ladderzip.rb', line 39

def set_compose_file
  @compose_file = Sc2::Paths.gem_root
    .join("lib", "docker_build", "docker-compose-ladderzip.yml")
    .expand_path.to_s
end

#start_containerObject



81
82
83
84
# File 'lib/sc2ai/cli/ladderzip.rb', line 81

def start_container
  cmd = "docker compose -f #{@compose_file} up bot -d --force-recreate"
  Kernel.system(cmd)
end

#stop_containerObject



155
156
157
158
# File 'lib/sc2ai/cli/ladderzip.rb', line 155

def stop_container
  cmd = "docker compose --progress=plain -f #{@compose_file} stop bot"
  Kernel.system(cmd)
end