Skip to content

Commit

Permalink
Merge pull request #82 from collectionspace/test-commands
Browse files Browse the repository at this point in the history
Narrow down/document config vars; Test profiles CLI commands
  • Loading branch information
kspurgin authored Oct 22, 2021
2 parents 04947c3 + 0dab4da commit 928861a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
23 changes: 14 additions & 9 deletions lib/cspace_config_untangler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ module CspaceConfigUntangler

# Change these variables to reflect your desired directory structure and main profile
default_datadir = '/Users/kristina/code/untangler-cspace-config/data'
default_configdir = File.join(default_datadir, 'configs')
default_templatedir = File.join(default_datadir, 'templates')
default_mapperdir = File.join(default_datadir, 'mappers')
default_main_profile_name = 'core'
# The publicly available web directory from which the CSV Importer will request mappers
default_mapper_uri_base = 'https://raw.githubusercontent.com/collectionspace/cspace-config-untangler/main/data/mappers'
# The last version of each profile that should get fancy column names created.
default_last_fancy_column_versions = {
Expand Down Expand Up @@ -54,24 +52,31 @@ def app_dir
File.realpath(File.join(File.dirname(__FILE__), '..'))
end

# Do not mess with these. Control subdirectories within them by passing in command output parameters as
# shown in the docs
default_configdir = File.join(default_datadir, 'configs')
default_templatedir = File.join(default_datadir, 'templates')
default_mapperdir = File.join(default_datadir, 'mappers')

setting :last_fancy_column_versions, default: default_last_fancy_column_versions, reader: true
setting :datadir, default: default_datadir, reader: true
setting :configdir, default: default_configdir, reader: true
setting :templatedir, default: default_templatedir, reader: true
setting :mapperdir, default: default_mapperdir, reader: true

config_file_names = Dir.new(default_configdir).children
.reject{ |e| e['readable'] }
.reject{ |e| e == '.keep' }
.map{ |fn| File.basename(fn).sub('.json', '') }

setting :profiles, default: config_file_names, reader: true
setting :main_profile_name, default: default_main_profile_name, reader: true
setting :log, default: logger, reader: true
setting :mapper_uri_base,
default: default_mapper_uri_base,
reader: true

def profiles
Dir.new(CCU.configdir).children
.reject{ |e| e['readable'] }
.reject{ |e| e == '.keep' }
.map{ |fn| File.basename(fn).sub('.json', '') }
end

def main_profile
Pathname.new(CCU.configdir)
.children(false)
Expand Down
1 change: 1 addition & 0 deletions lib/cspace_config_untangler/cli/helpers/profile_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Cli
module Helpers
class ProfileGetter
def self.call(opt_profiles)
return [CCU.main_profile] unless opt_profiles
return [CCU.main_profile] if opt_profiles.empty?

return self.all_profiles if opt_profiles == 'all'
Expand Down
35 changes: 17 additions & 18 deletions lib/cspace_config_untangler/cli/profiles_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module CspaceConfigUntangler
module Cli
class ProfilesCli < Thor
include CCU::Cli::Helpers
desc 'all', 'Print the names of all known profiles'
desc 'all', 'Print the names of all known profiles to screen'
def all
puts [CCU.main_profile, CCU.profiles].flatten.uniq.sort
say([CCU.main_profile, CCU.profiles].flatten.uniq.sort.join("\n"))
end

desc 'check', 'Print the names of profiles that will be processed'
desc 'check', 'Prints to screen the names of profiles that will be processed'
def check
profiles = get_profiles
puts profiles
say(profiles.join("\n"))
end

desc 'compare', 'Outputs a comparison of two profiles in CSV format'
Expand All @@ -33,20 +33,17 @@ def check
LONGDESC
option :output, desc: 'Path to directory in which to output file. Name of the file is hardcoded, using the names of the profiles.', default: CCU::datadir, aliases: '-o'
def compare
if options[:profiles] == 'all'
profiles = get_profiles
else
profiles = options[:profiles].split(',').map{ |p| p.strip }
end
profiles = get_profiles

if profiles.length > 2
puts "Can only compare two profiles at a time"
exit
say('Can only compare two profiles at a time')
elsif profiles.length == 1
puts "Needs two profiles to compare"
exit
say('Needs two profiles to compare')
else
CCU::ProfileComparison.new(profiles, options[:output]).write_csv
comparer = CCU::ProfileComparison.new(profiles, options[:output])
comparer.write_csv
message = "#{comparer.summary}\n\nWrote detailed report to: #{comparer.output}"
say(message)
end
end

Expand All @@ -70,18 +67,20 @@ def by_extension

desc 'main', 'Print the name of the main profile'
def main
puts CCU.main_profile
say(CCU.main_profile)
end

desc 'readable', 'Reformats (in place) JSON profile configs so that they are not one very long line. Non-destructive if run over JSON multiple times.'
def readable
message = []
get_profiles.each{ |p|
puts "Reformatting #{p} config"
profile = CCU::Profile.new(profile: p).config
message << "Reformatting #{p} config"
oldprofile = JSON.parse(File.read("#{CCU.configdir}/#{p}.json"))
File.open("#{CCU.configdir}/#{p}.json", 'w'){ |f|
f.puts JSON.pretty_generate(profile)
f.puts JSON.pretty_generate(oldprofile)
}
}
say(message.join("\n"))
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cspace_config_untangler/profile_comparison.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module CspaceConfigUntangler
class ProfileComparison
attr_reader :output
def initialize(profilearray, outputdir)
profiles = profilearray.map{ |p| CCU::Profile.new(profile: p) }
@profiles = profiles.map{ |p| p.name }
@output = "#{outputdir}/compare_#{@profiles[0]}_to_#{@profiles[1]}.csv"
@fields = profiles.map{ |p| p.fields.map{ |f| f.clean} }.map{ |p| by_path(p) }
@combined = combined_fields
@diff = diff_combined
@diff.each{ |k, arr| puts "#{k}: #{arr.size}" }
end

def write_csv
Expand All @@ -19,6 +19,10 @@ def write_csv
fields.each{ |f| csv << f.to_csv }
}
end

def summary
@diff.map{ |k, arr| "#{k}: #{arr.size}" }.join("\n")
end

private

Expand Down
2 changes: 1 addition & 1 deletion lib/cspace_config_untangler/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CspaceConfigUntangler
VERSION = '1.9.6'
VERSION = '1.9.7'
end

0 comments on commit 928861a

Please sign in to comment.