require 'yaml' module Sct class DevCommand @@dc = system("docker compose version", out: "/dev/null", err: "/dev/null") ? "docker compose" : "docker-compose" @@file = "docker-compose.dev.yml" def error message UI.error message exit 1 end def dc command, env = {} system env, "#{@@dc} -f ~/development/spend-cloud/docker-compose.yml #{command}" end def dc_dev command, env = {} system env, "#{@@dc} -f #{@@file} #{command}" end def manifest if !File.exist? @@file error "Could not find file '#{@@file}'." end YAML.load File.read @@file end def execute args, options services = manifest["services"].to_a if Sct::Helper.is_windows? host_machine_IP = (options.wsl_debugger ? `hostname -I | awk '{print $1}'` : `powershell.exe -c '(Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString' | dos2unix`).chomp elsif Sct::Helper.is_mac_os? host_machine_IP = `ipconfig getifaddr $(route get 8.8.8.8 | awk '/interface: / {print $2; }')`.chomp else host_machine_IP = `hostname -I | awk '{print $1}'`.chomp end env = { "PUID" => `id -u`.chomp, "PGID" => `id -g`.chomp, "HOST_MACHINE_IP" => host_machine_IP, "IDE_DEBUG_KEY" => "PHPSTORM", "IDE_DEBUG_SERVER" => "docker", } if options.pull return unless dc_dev "pull", env end if options.build return unless dc_dev "build #{options.pull ? "--pull" : ""}", env end if options.pull or options.build system "docker image prune -f" end for service, service_spec in services return unless (dc "stop #{service}") && (dc "rm --force #{service}") end if services.length == 1 service, service_spec = services.first container = service_spec["container_name"] command = service_spec["command"] || "" begin dc_dev "up --remove-orphans --force-recreate --always-recreate-deps", env rescue Interrupt # user pressed Ctrl+C, do nothing end dc_dev "down --remove-orphans", env else begin dc_dev "up --remove-orphans --force-recreate --always-recreate-deps", env rescue Interrupt # user pressed Ctrl+C, do nothing end dc_dev "down --remove-orphans", env end for service, service_spec in services dc "up --detach #{service}" end end end end