From 13851a980c395300ac47dfa4f7615e56281484a8 Mon Sep 17 00:00:00 2001 From: Guido Schweizer Date: Fri, 2 Dec 2022 19:42:16 +0100 Subject: [PATCH] adding gem file executable --- Gemfile | 1 - Gemfile.lock | 2 - aoc-helper-0.1.0.gem | Bin 0 -> 5632 bytes aoc.gemspec | 27 +++++++++++ app/main.rb | 16 ------- exe/aoc | 14 ++++++ lib/aoc.rb | 20 ++++++++ {app => lib}/tasks/new_day.rb | 12 +++-- {app => lib}/tasks/part_two.rb | 0 lib/tasks/templates.rb | 67 +++++++++++++++++++++++++++ license.txt | 1 + {app/templates => templates}/data | 0 {app/templates => templates}/impl.rb | 0 {app/templates => templates}/run.rb | 0 {app/templates => templates}/test.rb | 0 15 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 aoc-helper-0.1.0.gem create mode 100644 aoc.gemspec delete mode 100644 app/main.rb create mode 100755 exe/aoc create mode 100644 lib/aoc.rb rename {app => lib}/tasks/new_day.rb (67%) rename {app => lib}/tasks/part_two.rb (100%) create mode 100644 lib/tasks/templates.rb create mode 100644 license.txt rename {app/templates => templates}/data (100%) rename {app/templates => templates}/impl.rb (100%) rename {app/templates => templates}/run.rb (100%) rename {app/templates => templates}/test.rb (100%) diff --git a/Gemfile b/Gemfile index 6facc41..c9cb579 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,3 @@ source "https://rubygems.org" gem 'tty-prompt' gem 'tty-file' -gem "colorize", "~> 0.8.1" diff --git a/Gemfile.lock b/Gemfile.lock index aa8cd92..b525079 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,6 @@ GEM remote: https://rubygems.org/ specs: - colorize (0.8.1) diff-lcs (1.5.0) pastel (0.8.0) tty-color (~> 0.5) @@ -25,7 +24,6 @@ PLATFORMS arm64-darwin-21 DEPENDENCIES - colorize (~> 0.8.1) tty-file tty-prompt diff --git a/aoc-helper-0.1.0.gem b/aoc-helper-0.1.0.gem new file mode 100644 index 0000000000000000000000000000000000000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J-<^_Tu!`+t6zi(sMM~~~U!`@@0+&7Aedl}Q zdAn<#q=h{SSN9>w78J4-khFaB~NKFgvZ}=W=G`HqQ<>X<)oRX}7ayy2UFJ`C9 z_nnHSinapA;}}S_)rh*c=ueEwI}8N9g)KQMTKSVR#rlTt7pbC=TGIGcdjg?rbxP0Oq%!C2(TM+ z*u1D>4mKRK&5GA9RFvV2IccuZ$18et;9E@~SSs1BdgK_P#zHLpZZ0?t;wcX#cZCy!q^j1iB41cfW7-=1xA^D)XcjQcS;^L@6$ArDYQM)!tRDr4X~% z9zr_oxt(mzW!o1PGu?&`Pn5oAa5We?=ay9lGv4?GUtD4|Mm^PJ=yrwH=^~>FmO|$| zv<)LR(yjJdww4GaH98~v%OoT3(`Ku{ZoFCSfleEuM^?_fc&%XWdD}Pwd%!^Vc%y~2 z-!xg@T!AJ4eV7%Us-tnV(y>o#*1mh@&bc=Z%?ZO=r%lCgkI!99l3c+KrOT|Uo4G^^ zvEJ5ggM!$QYB2&5&yaGgSA*j(hZfzn;1vpTJGC(O2T)WnwUx0W}T+ zivaK+tr_m#`$>Th!r+yNt{A^R=NIs6YxFPrzkQq95A`3W3W5Ku|9|u@|C9B<)ql@a z*(V@9cH^s+g=(*IdaU?c+NT}#Jq=vd%k58h+$|uFH%+gTPPatt#G`aX`akuD=W8yl ze&||Cru3aTVBY}{R_ps4+Zo&}PnFh0!#W-0`dFsxtcg(K;lvW!jz@EUrRBstN1_3> zouugaQGWt%FMF+sjuTyEO7R#+70-EmIbu4zU501=UP%rCY02|e+Sgj!y6u3jQk z@(hNB5u`~F{?A)6Nw8DmIdq-*r+`pQfKbe^np(!m+TNm!5*3YB8 p>*{S%HAC-jIAoeBQMyEHj8tetO#cb6Kll7Z;3on<5%`NE@OMtP1J?im literal 0 HcmV?d00001 diff --git a/aoc.gemspec b/aoc.gemspec new file mode 100644 index 0000000..e3265ca --- /dev/null +++ b/aoc.gemspec @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +Gem::Specification.new do |spec| + spec.name = "aoc-helper" + spec.platform = Gem::Platform::RUBY + spec.version = "0.1.0" + spec.authors = ["Guido Schweizer"] + spec.email = ["guido.schweizer@posteo.de"] + spec.description = %q{helps to create basic folders for aoc} + spec.summary = %q{run aoc and follow instructions} + spec.homepage = "https://aoc.sgui.de" + spec.license = "MIT" + + spec.metadata["allowed_push_host"] = "https://rubygems.org" + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = "https://github.com/guidocodes/aoc-helper" + + spec.files = Dir.glob("lib/**/*", File::FNM_DOTMATCH) + spec.extra_rdoc_files = ["readme.md", "license.txt"] + spec.bindir = "exe" + spec.executables = ["aoc"] + spec.require_paths = ["lib"] + spec.required_ruby_version = ">= 3.0.0" + + spec.add_dependency "tty-file", "~> 0.10" + spec.add_dependency "tty-prompt", "~> 0.23" +end \ No newline at end of file diff --git a/app/main.rb b/app/main.rb deleted file mode 100644 index 6e37348..0000000 --- a/app/main.rb +++ /dev/null @@ -1,16 +0,0 @@ -require "tty-prompt" -require_relative "tasks/new_day" -require_relative "tasks/part_two" - -prompt = TTY::Prompt.new - -day = prompt.ask("Name the day (1-24)") do |q| - q.in("1-24") -end -selected = prompt.select("What do you want to do", "New day":0, "Add part 2 to a day":1) - -if selected == 0 - new_day(prompt, day) -elsif selected == 1 - part_two(prompt, day) -end diff --git a/exe/aoc b/exe/aoc new file mode 100755 index 0000000..a0a031c --- /dev/null +++ b/exe/aoc @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +lib_path = File.expand_path('../lib', __dir__) +$:.unshift(lib_path) if !$:.include?(lib_path) + +require("aoc") + +begin + AOC.run +rescue StandardError => err + puts "ERROR: #{err.message}" + exit 1 #err.status +end \ No newline at end of file diff --git a/lib/aoc.rb b/lib/aoc.rb new file mode 100644 index 0000000..4fe363b --- /dev/null +++ b/lib/aoc.rb @@ -0,0 +1,20 @@ +require "tty-prompt" +require_relative "tasks/new_day" +require_relative "tasks/part_two" + +class AOC + def self.run + prompt = TTY::Prompt.new + + day = prompt.ask("Name the day (1-24)") do |q| + q.in("1-24") + end + selected = prompt.select("What do you want to do", "New day":0, "Add part 2 to a day":1) + + if selected == 0 + new_day(prompt, day) + elsif selected == 1 + part_two(prompt, day) + end + end +end diff --git a/app/tasks/new_day.rb b/lib/tasks/new_day.rb similarity index 67% rename from app/tasks/new_day.rb rename to lib/tasks/new_day.rb index 4037794..337010b 100644 --- a/app/tasks/new_day.rb +++ b/lib/tasks/new_day.rb @@ -2,14 +2,20 @@ require "tty-prompt" require "tty-file" require "ostruct" +require_relative("templates") + def new_day(prompt, day) input = prompt.multiline("Example input").join("") print input result = prompt.ask("Example result") day_name = "day_#{day}" - - TTY::File.copy_directory("app/templates","#{day_name}") + + TTY::File.create_dir(day_name) + TTY::File.create_file("#{day_name}/data") + TTY::File.create_file("#{day_name}/impl.rb", IMPL_RB) + TTY::File.create_file("#{day_name}/run.rb", RUN_RB) + TTY::File.create_file("#{day_name}/test.rb", TEST_RB) TTY::File.replace_in_file "#{day_name}/run.rb", /%day_name%/, day_name TTY::File.replace_in_file "#{day_name}/test.rb", /%day_name%/, day_name @@ -18,4 +24,4 @@ def new_day(prompt, day) puts "Done - run with:" puts "ruby #{day_name}/test.rb" -end \ No newline at end of file +end diff --git a/app/tasks/part_two.rb b/lib/tasks/part_two.rb similarity index 100% rename from app/tasks/part_two.rb rename to lib/tasks/part_two.rb diff --git a/lib/tasks/templates.rb b/lib/tasks/templates.rb new file mode 100644 index 0000000..942f9f6 --- /dev/null +++ b/lib/tasks/templates.rb @@ -0,0 +1,67 @@ + +TEST_RB = <<~'TEST' +require_relative "impl" + +INPUT = <<~IN +%example_input% +IN + +RESULT = %example_result% + +def test_example + parsed = parse(INPUT) + result = calculate(parsed) + + if result == RESULT + puts "Test successful. Now run with real input" + puts "ruby %day_name%/run.rb" + else + puts "Test failed" + puts "expected \"#{RESULT}\" got \"#{result}\"" + end +end + +test_example +TEST + +RUN_RB = <<~'RUN' +require_relative "impl" + +def print(result) + if result.is_a? Array + result.map! {|l| "#{l}\n" } + end + + puts result +end + +def run + puts "running your implementation" + input = File.new("%day_name%/data").read + data = parse(input) + result = calculate(data) + print(result) +end + +run +RUN + +IMPL_RB = <<~'IMPL' +#prepare the input which is a string containing new lines +def parse(input) + data = [] + input.each_line do |line| + data << line + end + data +end + +# result should a single string or integer +def calculate(data) + result = "" + data.each do |d| + result = d + end + result +end +IMPL \ No newline at end of file diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..8ab70c0 --- /dev/null +++ b/license.txt @@ -0,0 +1 @@ +MIT \ No newline at end of file diff --git a/app/templates/data b/templates/data similarity index 100% rename from app/templates/data rename to templates/data diff --git a/app/templates/impl.rb b/templates/impl.rb similarity index 100% rename from app/templates/impl.rb rename to templates/impl.rb diff --git a/app/templates/run.rb b/templates/run.rb similarity index 100% rename from app/templates/run.rb rename to templates/run.rb diff --git a/app/templates/test.rb b/templates/test.rb similarity index 100% rename from app/templates/test.rb rename to templates/test.rb