Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 15, 2021
1 parent 921509b commit dba2d15
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ENV GEM_HOME=${LAMBDA_TASK_ROOT}
COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY lib/ .
COPY lib/ lib/
COPY tracks/ tracks/

CMD [ "app.Exercism::CountLinesOfCode.process" ]
CMD [ "lib/lines_of_code_counter.LinesOfCodeCounter.process" ]
10 changes: 5 additions & 5 deletions bin/run-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ container_port=9876
# Create the output directory if it doesn't exist
mkdir -p "${output_dir}"

echo "${track_slug}/${exercise_slug}: counting lines of code..."

# Build the Docker image
docker build --rm -t exercism/lines-of-code-counter .
docker build --progress=plain --rm -t exercism/lines-of-code-counter .

# Run the Docker image using the settings mimicking the production environment
container_id=$(docker run \
Expand All @@ -44,10 +42,12 @@ container_id=$(docker run \
--mount type=bind,src="${output_dir}",dst=/output \
exercism/lines-of-code-counter)

echo "${track_slug}/${exercise_slug}: counting lines of code..."

# Call the function with the correct JSON event payload
event_json=$(jq -n --arg t "${track_slug}" --arg e "${exercise_slug}" '{track: $t, exercise: $e, solution: "/solution", output: "/output"}')
curl --silent --output /dev/null -XPOST http://localhost:${container_port}/2015-03-31/functions/function/invocations -d "${event_json}"

docker stop $container_id > /dev/null
echo "${track_slug}/${exercise_slug}: done"

echo "${track_slug}/${exercise_slug}: done"
docker stop $container_id > /dev/null
2 changes: 2 additions & 0 deletions bin/run-tests-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ for test_dir in tests/${track_slug}/${exercise_slug}; do
counts_file_path="${test_dir_path}/counts.json"
expected_counts_file_path="${test_dir_path}/expected_counts.json"

rm -rf "${counts_file_path}"

bin/run-in-docker.sh "${track_name}" "${exercise_name}" "${test_dir_path}" "${test_dir_path}"

echo "${track_name}/${exercise_name}: comparing counts.json to expected_counts.json"
Expand Down
2 changes: 2 additions & 0 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ for test_dir in tests/${track_slug}/${exercise_slug}; do
counts_file_path="${test_dir_path}/counts.json"
expected_counts_file_path="${test_dir_path}/expected_counts.json"

rm -rf "${counts_file_path}"

bin/run.sh "${track_name}" "${exercise_name}" "${test_dir_path}" "${test_dir_path}"

echo "${track_name}/${exercise_name}: comparing counts.json to expected_counts.json"
Expand Down
4 changes: 2 additions & 2 deletions bin/run.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env ruby

require("./lib/app")
require("./lib/lines_of_code_counter")

event = {
"track" => ARGV[0],
"exercise" => ARGV[1],
"solution" => ARGV[2],
"output" => ARGV[3]
}
Exercism::CountLinesOfCode.process(event: event, context: {})
LinesOfCodeCounter.process(event: event, context: {})
91 changes: 0 additions & 91 deletions lib/app.rb

This file was deleted.

13 changes: 13 additions & 0 deletions lib/lines_of_code_counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "json"
require "mandate"
require "fileutils"
require "./lib/lines_of_code_counter/count_lines_of_code"
require "./lib/lines_of_code_counter/exercise"
require "./lib/lines_of_code_counter/ignore_file"
require "./lib/lines_of_code_counter/process_request"

module LinesOfCodeCounter
def self.process(event:, context:)
ProcessRequest.(event, context)
end
end
40 changes: 40 additions & 0 deletions lib/lines_of_code_counter/count_lines_of_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class CountLinesOfCode
include Mandate

initialize_with :exercise

def call
{
code: code,
blanks: blanks,
comments: comments,
files: files
}
end

private
memoize
def report
JSON.parse(`tokei #{exercise.dir} --output json`, symbolize_names: true)
end

def code
report[:Total][:code]
end

def blanks
report[:Total][:blanks]
end

def comments
report[:Total][:comments]
end

def files
report[:Total][:children].values.
flatten.
map {|child| child[:name].delete_prefix("#{exercise.dir}/") }.
sort.
to_a
end
end
36 changes: 36 additions & 0 deletions lib/lines_of_code_counter/exercise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Exercise
include Mandate

attr_reader :dir

def initialize(dir)
@dir = dir
end

def solution_files
config[:files][:solution].to_a
end

def test_files
config[:files][:test].to_a
end

def editor_files
config[:files][:editor].to_a
end

def exemplar_files
config[:files][:exemplar].to_a
end

def example_files
config[:files][:example].to_a
end

private
memoize
def config
filepath = File.join(dir, ".meta", "config.json")
JSON.parse(File.read(filepath), symbolize_names: true)
end
end
43 changes: 43 additions & 0 deletions lib/lines_of_code_counter/ignore_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class IgnoreFile
include Mandate

initialize_with :track, :exercise

def filepath
File.join(exercise.dir, ".tokeignore")
end

def content
rules = track_specific_rules? ? track_specific_rules : default_rules
rules.compact.join("\n")
end

memoize
def track_specific_rules?
File.exist?(track_ignore_filepath)
end

def track_specific_rules
[
*File.readlines(track_ignore_filepath),
*exercise.test_files,
*exercise.example_files,
*exercise.exemplar_files,
*exercise.editor_files,
"counts.json",
"expected_counts.json"
]
end

def default_rules
[
"*",
*exercise.solution_files.map {|s|"!#{s}"}
]
end

private
def track_ignore_filepath
"tracks/#{track}.ignore"
end
end
39 changes: 39 additions & 0 deletions lib/lines_of_code_counter/process_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ProcessRequest
include Mandate

initialize_with :event, :content

def call
File.write(ignore_file.filepath, ignore_file.content)
counts = CountLinesOfCode.(exercise)
File.write(counts_filepath, counts.to_json)
# FileUtils.rm(ignore_file.filepath)
end

private
def track
event["track"]
end

def solution_dir
event["solution"]
end

def output_dir
event["output"]
end

def counts_filepath
File.join(output_dir, "counts.json")
end

memoize
def exercise
Exercise.new(solution_dir)
end

memoize
def ignore_file
IgnoreFile.new(track, exercise)
end
end

0 comments on commit dba2d15

Please sign in to comment.