Skip to content
Permalink
Browse files

Move shared Ruby scripts to common location. Wrap in module/namespace. (

  • Loading branch information
markfickett committed Sep 12, 2017
1 parent 17a732f commit 5e5212763fb72b32ce4dc2c55c11f2ffe799f9d8
@@ -46,10 +46,10 @@ jobs:
command: |
git submodule update --init
- run:
working_directory: ~/workbench/ui
working_directory: ~/workbench
name: Download Swagger CLI
command: |
ruby -r ./libproject/download-swagger-codegen-cli.rb -e download_swagger_codegen_cli
ruby -r ./libproject/swagger.rb -e Workbench::Swagger.download_swagger_codegen_cli
- restore_cache:
keys:
- ui-cache-{{ checksum "~/workbench/ui/package.json" }}
@@ -21,7 +21,7 @@
/ui/**/*.js
/ui/**/*.map
/ui/dist/**
/ui/libproject/swagger-codegen-cli.jar
/libproject/swagger-codegen-cli.jar
/ui/src/generated/*
cloud_sql_proxy
node_modules
@@ -1,6 +1,3 @@
[submodule "api/libproject/utils"]
path = api/libproject/utils
url = https://github.com/dmohs/project-management.git
[submodule "ui/libproject/utils"]
path = ui/libproject/utils
[submodule "libproject/utils"]
path = libproject/utils
url = https://github.com/dmohs/project-management.git
@@ -1,16 +1,9 @@
require_relative "utils/common"
require_relative "../../libproject/utils/common"
require "io/console"
require "json"
require "optparse"
require "tempfile"

def ensure_git_hooks()
common = Common.new
unless common.capture_stdout(%W{git config --get core.hooksPath}).chomp == "hooks"
common.run_inline %W{git config core.hooksPath hooks}
end
end

class ProjectAndAccountOptions
attr_accessor :project
attr_accessor :account
@@ -48,12 +41,10 @@ def parse args
end
end

def dev_up(args)
def dev_up(*args)
common = Common.new
common.docker.requires_docker

ensure_git_hooks

at_exit { common.run_inline %W{docker-compose down} }
common.status "Starting database..."
common.run_inline %W{docker-compose up -d db}
@@ -78,22 +69,22 @@ def dev_up(args)
common.run_inline_swallowing_interrupt %W{docker-compose up api}
end

def connect_to_db(args)
def connect_to_db(*args)
common = Common.new
common.docker.requires_docker

cmd = "MYSQL_PWD=root-notasecret mysql --database=workbench"
common.run_inline %W{docker-compose exec db sh -c #{cmd}}
end

def docker_clean(args)
def docker_clean(*args)
common = Common.new
common.docker.requires_docker

common.run_inline %W{docker-compose down --volumes}
end

def rebuild_image(args)
def rebuild_image(*args)
common = Common.new
common.docker.requires_docker

@@ -237,21 +228,21 @@ def run_with_cloud_sql_proxy(args, command, proc)
})
end

def drop_cloud_db(args)
def drop_cloud_db(*args)
run_with_cloud_sql_proxy(args, "drop-cloud-db", lambda { |project, account, creds_file|
do_drop_db(project)
})
end

def connect_to_cloud_db(args)
def connect_to_cloud_db(*args)
run_with_cloud_sql_proxy(args, "connect-to-cloud-db", lambda { |project, account, creds_file|
read_db_vars(project)
system("mysql -u \"workbench\" -p\"#{ENV["WORKBENCH_DB_PASSWORD"]}\" --host 127.0.0.1 "\
"--port 3307 --database #{ENV["DB_NAME"]}")
})
end

def run_cloud_migrations(args)
def run_cloud_migrations(*args)
run_with_cloud_sql_proxy(args, "run-cloud-migrations", lambda { |project, account, creds_file|
puts "Running migrations..."
do_run_migrations(project)
@@ -309,49 +300,49 @@ def create_db_creds(args)
Common.register_command({
:invocation => "dev-up",
:description => "Brings up the development environment.",
:fn => lambda { |args| dev_up(args) }
:fn => lambda { |*args| dev_up(*args) }
})

Common.register_command({
:invocation => "connect-to-db",
:description => "Connect to the running database via mysql.",
:fn => lambda { |args| connect_to_db(args) }
:fn => lambda { |*args| connect_to_db(*args) }
})

Common.register_command({
:invocation => "docker-clean",
:description => \
"Removes docker containers and volumes, allowing the next `dev-up` to\n" \
"start from scratch (e.g., the database will be re-created).",
:fn => lambda { |args| docker_clean(args) }
:fn => lambda { |*args| docker_clean(*args) }
})

Common.register_command({
:invocation => "rebuild-image",
:description => "Re-builds the dev docker image (necessary when Dockerfile is updated).",
:fn => lambda { |args| rebuild_image(args) }
:fn => lambda { |*args| rebuild_image(*args) }
})

Common.register_command({
:invocation => "create-db-creds",
:description => "Creates database credentials in a file in GCS; accepts project and account args",
:fn => lambda { |args| create_db_creds(args) }
:fn => lambda { |*args| create_db_creds(*args) }
})

Common.register_command({
:invocation => "drop-cloud-db",
:description => "Drops the Cloud SQL database for the specified project",
:fn => lambda { |args| drop_cloud_db(args) }
:fn => lambda { |*args| drop_cloud_db(*args) }
})

Common.register_command({
:invocation => "run-cloud-migrations",
:description => "Runs database migrations on the Cloud SQL database for the specified project.",
:fn => lambda { |args| run_cloud_migrations(args) }
:fn => lambda { |*args| run_cloud_migrations(*args) }
})

Common.register_command({
:invocation => "connect-to-cloud-db",
:description => "Connect to a Cloud SQL database via mysql.",
:fn => lambda { |args| connect_to_cloud_db(args) }
:fn => lambda { |*args| connect_to_cloud_db(*args) }
})
@@ -1,32 +1,6 @@
#!/usr/bin/env ruby

# `git clone` includes submodule folders but nothing else.
unless File.exists? "libproject/utils/README.md"
unless system(*%W{git submodule update --init libproject/utils})
STDERR.puts "`git submodule update` failed."
exit 1
end
end

require_relative "libproject/utils/common"

#
# Custom script files
#

require_relative "../libproject/workbench"
require_relative "libproject/devstart.rb"

#
# End custom script files
#

common = Common.new

if ARGV.length == 0 or ARGV[0] == "--help"
common.print_usage
exit 0
end

command = ARGV.first

common.handle_or_die(command, ARGV.drop(1))
Workbench.handle_argv_or_die(__FILE__)
@@ -0,0 +1,18 @@
require "optparse"
require_relative "../../libproject/utils/common"
require_relative "../../libproject/swagger"

def swagger_regen()
Workbench::Swagger.download_swagger_codegen_cli

common = Common.new
common.run_inline %W{
java -jar #{Workbench::Swagger::SWAGGER_CODEGEN_CLI_JAR}
generate --lang python --input-spec #{Workbench::Swagger::SWAGGER_SPEC} --output py/}
end

Common.register_command({
:invocation => "swagger-regen",
:description => "rebuilds the Swagger-generated client libraries",
:fn => Proc.new { |*args| swagger_regen(*args) }
})
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require_relative "../libproject/workbench"
require_relative "libproject/devstart.rb"

Workbench.handle_argv_or_die(__FILE__)
@@ -0,0 +1,31 @@
require "open-uri"
require_relative "utils/common"
require_relative "workbench"

module Workbench
module Swagger
SWAGGER_CODEGEN_CLI_JAR = File.join(
Workbench::WORKBENCH_ROOT, "libproject", "swagger-codegen-cli.jar")
SWAGGER_SPEC = File.join(
Workbench::WORKBENCH_ROOT, "api", "src", "main", "resources", "workbench.yaml")

def download(url, path)
File.open(path, "wb") do |f|
IO.copy_stream(open(url), f)
end
end
module_function :download

def download_swagger_codegen_cli()
unless File.exist?(SWAGGER_CODEGEN_CLI_JAR)
common = Common.new
jar_url = "https://storage.googleapis.com" +
"/swagger-codegen-cli/swagger-codegen-cli-2.3.0-20170814.101630-90.jar"
common.status "#{jar_url} > #{SWAGGER_CODEGEN_CLI_JAR}..."
download(jar_url, SWAGGER_CODEGEN_CLI_JAR)
end
end
module_function :download_swagger_codegen_cli

end
end
File renamed without changes.
@@ -0,0 +1,43 @@
require_relative "utils/common"

module Workbench
WORKBENCH_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

def check_submodules()
# `git clone` includes submodule folders but nothing else.
unless File.exists? File.join(WORKBENCH_ROOT, "libproject", "utils", "README.md")
unless system(*%W{git submodule update --init})
common.error "`git submodule update` failed."
exit 1
end
end
end
module_function :check_submodules

def ensure_git_hooks()
common = Common.new
unless common.capture_stdout(%W{git config --get core.hooksPath}).chomp == "hooks"
common.run_inline %W{git config core.hooksPath hooks}
end
end
module_function :ensure_git_hooks

def handle_argv_or_die(main_filename)
Dir.chdir(File.dirname(main_filename))

check_submodules
ensure_git_hooks

common = Common.new
if ARGV.length == 0 or ARGV[0] == "--help"
common.print_usage
exit 0
end

command = ARGV.first
args = ARGV.drop(1)

common.handle_or_die(command, *args)
end
module_function :handle_argv_or_die
end
@@ -1,13 +1,9 @@
require "optparse"
require_relative "download-swagger-codegen-cli"
require_relative "utils/common"
# UI project management commands and command-line flag definitions.

def ensure_git_hooks()
common = Common.new
unless common.capture_stdout(%W{git config --get core.hooksPath}).chomp == "hooks"
common.run_inline %W{git config core.hooksPath hooks}
end
end
require "optparse"
require_relative "../../libproject/utils/common"
require_relative "../../libproject/workbench"
require_relative "../../libproject/swagger"

def install_dependencies()
common = Common.new
@@ -20,9 +16,7 @@ def swagger_regen()
common = Common.new
common.docker.requires_docker

unless File.exist?(SWAGGER_CODEGEN_CLI_JAR)
download_swagger_codegen_cli
end
Workbench::Swagger.download_swagger_codegen_cli

common.run_inline %W{docker-compose run --rm ui npm run codegen}
end
@@ -54,14 +48,8 @@ def dev_up(*args)

options = DevUpOptions.new.parse(args)

ensure_git_hooks

install_dependencies

unless File.exist?(SWAGGER_CODEGEN_CLI_JAR)
download_swagger_codegen_cli
end

ENV["ENV_FLAG"] = options.env == "local" ? "" : "--environment=#{options.env}"
at_exit { common.run_inline %W{docker-compose down} }
common.run_inline %W{docker-compose run -d --service-ports tests}
@@ -73,10 +61,6 @@ def dev_up(*args)
common.run_inline %W{docker-compose run --rm --service-ports ui}
end

def clean_git_hooks()
common.run_inline %W{find ../.git/hooks -type l -delete}
end

def rebuild_image()
common = Common.new
common.docker.requires_docker

This file was deleted.

Submodule utils deleted from 7b342b
@@ -10,7 +10,7 @@
"lint": "ng lint",
"e2e": "ng e2e",
"codegen-clean": "rm -rf src/generated",
"codegen": "npm run codegen-clean && java -jar libproject/swagger-codegen-cli.jar generate --lang typescript-angular --input-spec ../api/src/main/resources/workbench.yaml --output src/generated --additional-properties ngVersion=2"
"codegen": "npm run codegen-clean && java -jar ../libproject/swagger-codegen-cli.jar generate --lang typescript-angular --input-spec ../api/src/main/resources/workbench.yaml --output src/generated --additional-properties ngVersion=2"
},
"private": true,
"dependencies": {

0 comments on commit 5e52127

Please sign in to comment.
You can’t perform that action at this time.