Skip to content

Commit

Permalink
Fully implement and test config/generator/generate/write
Browse files Browse the repository at this point in the history
  • Loading branch information
azimux committed Mar 17, 2024
1 parent 7ef4fce commit 38eca29
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GIT

GIT
remote: https://github.com/foobara/files-generator.git
revision: 8e9439014484892bb49a1d720399d924f853baea
revision: e51ee030f697e7e7669df7ad329c407b3acead11
specs:
foobara-files-generator (0.1.0)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
RSpec.describe Foobara::Generators::EmptyTypescriptReactProjectGenerator::GenerateEmptyTypescriptReactProject do
let(:project_dir) { "SomePrefix::SomeOrg" }
let(:project_dir) { "test-project" }

let(:inputs) do
{
project_dir:,
description: "whatever"
project_dir:
}
end
let(:empty_typescript_react_project) { described_class.new(inputs) }
Expand All @@ -14,8 +13,6 @@
it "generates a empty_typescript_react_project" do
expect(outcome).to be_success

empty_typescript_react_project_file = result["src/some_prefix/some_org.rb"]
expect(empty_typescript_react_project_file).to include("module SomeOrg")
expect(empty_typescript_react_project_file).to include("module SomePrefix")
expect(result.keys).to contain_exactly("tsconfig.json", ".env", ".eslintrc.js")
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RSpec.describe Foobara::Generators::EmptyTypescriptReactProjectGenerator::WriteEmptyTypescriptReactProjectToDisk do
let(:command) { described_class.new(inputs) }
let(:outcome) { empty_typescript_react_project.run }
let(:outcome) { command.run }
let(:result) { outcome.result }
let(:errors) { outcome.errors }
let(:inputs) do
Expand All @@ -11,26 +11,22 @@
end
let(:empty_typescript_react_project_config) do
{
project_dir:,
description: "whatever"
project_dir:
}
end
let(:project_dir) { "SomeOrg" }
let(:output_directory) { "#{__dir__}/../../../tmp/empty_typescript_react_project_test_suite_output" }
let(:project_dir) { "test-project" }
let(:output_directory) { "#{__dir__}/../../../tmp/rspec-output" }

before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(described_class).to receive(:git_commit).and_return(nil)
allow_any_instance_of(described_class).to receive(:rubocop_autocorrect).and_return(nil)
# rubocop:enable RSpec/AnyInstance
allow(command).to receive_messages(git_commit: nil, rubocop_autocorrect: nil)
FileUtils.rm_rf output_directory
end

describe "#run" do
it "contains base files" do
expect(outcome).to be_success

expect(command.paths_to_source_code.keys).to include("src/some_org.rb")
expect(File.exist?("#{output_directory}/#{project_dir}/.eslintrc.js")).to be(true)
end
end

Expand All @@ -44,7 +40,7 @@

it "writes files to the current directory" do
command.cast_and_validate_inputs
expect(command.output_directory).to eq(".")
expect(command.output_parent_directory).to eq(".")
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions src/empty_typescript_react_project_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ module EmptyTypescriptReactProjectGenerator
class EmptyTypescriptReactProjectConfig < Foobara::Model
attributes do
project_dir :string, :required
description :string, :allow_nil
end

attr_accessor :module_path

def initialize(attributes = nil, options = {})
project_dir = attributes[:project_dir]
description = attributes[:description]

module_path = project_dir.split("::")

super(
{
project_dir:,
description:
},
options
)

self.module_path = module_path
end
end
end
Expand Down
23 changes: 2 additions & 21 deletions src/empty_typescript_react_project_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,15 @@ class << self
def manifest_to_generator_classes(manifest)
case manifest
when EmptyTypescriptReactProjectConfig
[
Generators::EmptyTypescriptReactProjectGenerator
]
# Nothing to do yet re: rendering templates. Everything is untemplated so far.
[]
else
# :nocov:
raise "Not sure how build a generator for a #{manifest}"
# :nocov:
end
end
end

def template_path
["src", "organization.rb.erb"]
end

def target_path
*path, file = module_path.map { |part| Util.underscore(part) }

file = "#{file}.rb"

["src", *path, file]
end

alias empty_typescript_react_project_config relevant_manifest

def templates_dir
"#{__dir__}/../templates"
end
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions src/generate_empty_typescript_react_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ class MissingManifestError < RuntimeError; end
inputs EmptyTypescriptReactProjectConfig

def execute
include_non_templated_files

add_initial_elements_to_generate

each_element_to_generate do
# currently there are no templated files to write and so this code path isn't hit
# :nocov:
generate_element
# :nocov:
end

paths_to_source_code
Expand All @@ -30,10 +35,7 @@ def base_generator

# TODO: delegate this to base_generator
def templates_dir
# TODO: implement this?
# :nocov:
"#{__dir__}/../templates"
# :nocov:
end

def add_initial_elements_to_generate
Expand Down
34 changes: 23 additions & 11 deletions src/write_empty_typescript_react_project_to_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def generator_key
inputs do
empty_typescript_react_project_config EmptyTypescriptReactProjectConfig, :required
# TODO: should be able to delete this and inherit it
output_directory :string
output_directory :string, default: "."
end

def execute
Expand All @@ -27,12 +27,19 @@ def execute
stats
end

# A bit confusing... we need to write the files to the output_directory/project_dir
# and the code that writes the generated files assumes that output_directory contains the place
# to write the files not the place to initiate the project
def output_directory
inputs[:output_directory] || default_output_directory
project_directory
end

def default_output_directory
"."
def output_parent_directory
inputs[:output_directory]
end

def project_directory
"#{output_parent_directory}/#{empty_typescript_react_project_config.project_dir}"
end

def generate_file_contents
Expand All @@ -48,8 +55,11 @@ def run_pre_generation_tasks
def run_npx_create_react_app
puts "created empty project with create-react-app..."

Dir.chdir output_directory do
cmd = "npx create-react-app --template typescript whatever-frontend"
cmd = "npx create-react-app --template typescript #{empty_typescript_react_project_config.project_dir}"

FileUtils.mkdir_p output_parent_directory

Dir.chdir output_parent_directory do
run_cmd_and_write_output(cmd)
end
end
Expand All @@ -65,18 +75,20 @@ def add_necessary_dev_dependencies_for_eslint
"eslint-plugin-promise@^6.1.1 " \
"typescript@^4.0.0 "

run_cmd_and_write_output(cmd)
Dir.chdir project_directory do
run_cmd_and_write_output(cmd)
end
end

def run_post_generation_tasks
Dir.chdir output_directory do
eslint_fix
end
eslint_fix
end

def eslint_fix
cmd = "npx eslint 'src/**/*.{js,jsx,ts,tsx}' --fix"
run_cmd_and_write_output(cmd)
Dir.chdir project_directory do
run_cmd_and_write_output(cmd)
end
end
end
end
Expand Down

0 comments on commit 38eca29

Please sign in to comment.