module Soaspec # Help with tasks common to soaspec executables module ExeHelpers require 'fileutils' # @param [String] filename Name of the file to create # @param [String] content Content to place inside file def create_file(filename: nil, content: nil, ignore_if_present: false) #filename = options[:filename] raise 'Need to pass filename' unless filename #content = options[:content] raise 'Need to pass contents to insert into file' unless content if File.exist? filename old_content = File.read(filename) if old_content != content && !ignore_if_present warn "!! #{filename} already exists and differs from template" end else File.open(filename, 'w') { |f| f.puts content } puts 'Created: ' + filename end end def create_folder(folder) if File.exist? folder unless File.directory? folder warn "!! #{folder} already exists and is not a directory" end else FileUtils.mkdir folder puts "Created folder: #{folder}/" end end def rake_content <<-EOF # The list of task for a Rake file can be seen with `rake -T` require 'rspec/core/rake_task' # See See https://relishapp.com/rspec/rspec-core/docs/command-line/rake-task for details # This runs `rspec` command with the following options. Type `rake spec` to run this task RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = "spec/*_spec.rb" # Run all specs in 'spec' folder ending in '_spec' # Next line shows output on the screen, Junit xml report and an HTML report t.rspec_opts = "--format documentation --format RspecJunitFormatter --out logs/spec.xml --format html --out logs/spec.html" t.fail_on_error = false end task :default => :spec # This runs the 'spec' task by default when no task is mentioned. E.g., if only `rake` is typed EOF end def gem_content <<-EOF source 'https://rubygems.org' gem 'data_magic' gem 'require_all' gem 'rspec_junit_formatter' gem 'rake' gem 'soaspec' EOF end def spec_helper_content <<-EOF require 'soaspec' require 'require_all' require_all 'lib' require 'data_magic' include DataMagic # Used as example of loading data smartly. Use 'data_for' method to load yml data RSpec.configure do |config| # This will make backtrace much shorter by removing many lines from rspec failure message config.backtrace_exclusion_patterns = [ /rspec/ ] end EOF end # Convert key in camelcase to underscore separated (snakecase) def underscore_key(key) key.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr('-', '_') .gsub(/\s/, '_') .gsub(/__+/, '_') .downcase end end end