Create a gist now

Instantly share code, notes, and snippets.

@arfon /generate
Last active Apr 21, 2017

What would you like to do?
Let's try and generate some codemeta files.
#!/usr/bin/ruby
# For an OO language, this is distinctly procedural. Should probably fix that.
require 'json'
details = Hash.new({})
capture_params = [
{ :name => "title", :message => "Enter project name." },
{ :name => "url", :message => "Enter the URL of the project repository." },
{ :name => "description", :message => "Enter the (short) project description." },
{ :name => "license", :message => "Enter the license this software shared under. (hit enter to skip)\nFor example MIT, BSD, GPL v3.0, Apache 2.0" },
{ :name => "doi", :message => "Enter the DOI of the archived version of this code. (hit enter to skip)\nFor example http://dx.doi.org/10.6084/m9.figshare.828487" },
{ :name => "keywords", :message => "Enter keywords that should be associated with this project (hit enter to skip)\nComma-separated, for example: turkey, chicken, pot pie" },
{ :name => "version", :message => "Enter the version of your software (hit enter to skip)\nSEMVER preferred: http://semver.org e.g. v1.0.0" }
]
puts "I'm going to try and help you prepare some things for your JOSS submission"
puts "If all goes well then we'll have a nice codemeta.json file soon..."
puts ""
puts "************************************"
puts "* First, some basic details *"
puts "************************************"
puts ""
# Loop through the desired captures and print out for clarity
capture_params.each do |param|
puts param[:message]
print "> "
input = gets
details[param[:name]] = input.chomp
puts ""
puts "OK, your project has #{param[:name]}: #{input}"
puts ""
end
puts ""
puts "************************************"
puts "* Experimental stuff *"
puts "************************************"
puts ""
puts "Would you like me to try and build a list of authors for you?"
puts "(You need to be running this script in a git repository for this to work)"
print "> (Y/N)"
answer = gets.chomp
case answer.downcase
when "y", "yes"
# Use git shortlog to extract a list of author names and commit counts.
# Note we don't extract emails here as there's often different emails for
# each user. Instead we capture emails at the end.
git_log = `git shortlog --summary --numbered --no-merges`
# ["252\tMichael Jackson", "151\tMC Hammer"]
authors_and_counts = git_log.split("\n").map(&:strip)
authors_and_counts.each do |author_count|
count, author = author_count.split("\t").map(&:strip)
puts "Looks like #{author} made #{count} commits"
puts "Add them to the output?"
print "> (Y/N)"
answer = gets.chomp
# If a user chooses to add this author to the output then we ask for some
# additional information including their email, ORCID and affiliation.
case answer.downcase
when "y", "yes"
puts "What is #{author}'s email address? (hit enter to skip)"
print "> "
email = gets.chomp
puts "What is #{author}'s ORCID? (hit enter to skip)"
puts "For example: http://orcid.org/0000-0000-0000-0000"
print "> "
orcid = gets.chomp
puts "What is #{author}'s affiliation? (hit enter to skip)"
print "> "
affiliation = gets.chomp
details['authors'].merge!(author => { 'commits' => count,
'email' => email,
'orcid' => orcid,
'affiliation' => affiliation })
when "n", "no"
puts "OK boss..."
puts ""
end
end
when "n", "no"
puts "OK boss..."
puts ""
end
puts "Reticulating splines"
5.times do
print "."
sleep 0.5
end
puts ""
puts "Generating some JSON goodness..."
# TODO: work out how to use some kind of JSON template here.
# Build the output list of authors from the inputs we've collected.
output_authors = []
details['authors'].each do |author_name, values|
entry = {
"@id" => values['orcid'],
"@type" => "Person",
"email" => values['email'],
"name" => author_name,
"affiliation" => values['affiliation']
}
output_authors << entry
end
# TODO: this is currently a static template (written out here). It would be good
# to do something smarter here.
output = {
"@context" => "https://raw.githubusercontent.com/codemeta/codemeta/master/codemeta.jsonld",
"@type" => "Code",
"author" => output_authors,
"identifier" => details['doi'],
"codeRepository" => details['url'],
"datePublished" => Time.now.strftime("%Y-%m-%d"),
"dateModified" => Time.now.strftime("%Y-%m-%d"),
"dateCreated" => Time.now.strftime("%Y-%m-%d"),
"description" => details['description'],
"keywords" => details['keywords'],
"license" => details['license'],
"title" => details['title'],
"version" => details['version']
}
File.open('codemeta.json', 'w') {|f| f.write(JSON.pretty_generate(output)) }
{
"@context": "https://raw.githubusercontent.com/codemeta/codemeta/master/codemeta.jsonld",
"@type": "Code",
"author": [
{
"@id": "http://orcid.org/0000-0002-3957-2474",
"@type": "Person",
"email": "arfon@github.com",
"name": "Arfon Smith",
"affiliation": "GitHub Inc."
}
],
"identifier": "http://dx.doi.org/10.6084/m9.figshare.828487",
"codeRepository": "https://github.com/arfon/fidgit",
"datePublished": "2016-01-28",
"dateModified": "2016-01-28",
"dateCreated": "2016-01-28",
"description": "An ungodly union of GitHub and figshare",
"keywords": "software, archiving, credit for code",
"license": "MIT",
"title": "fidgit",
"version": "v0.1.0"
}

band commented Jun 7, 2016

I suggest the following copyedits (rationale: fewer words simpler sentences)
(1) the questions can be rephrased as requests. For example, in place of "What is the name of this project?" use "Enter project name." (This change makes all the prompts the same pattern.) If you prefer questions, then "What is the project name?" is shorter.
(2) If the current phrasing is OK, then replace "What's" with "What is" in the DOI line.
(3) In the current form keywords and version lines need "?" marks.

Owner

arfon commented Jun 8, 2016

👍 thanks @band.

mbjones commented Jul 30, 2016

@arfon Your script points people at the master branch for codemeta.jsonld, which is a moving target. Please instead ask people to point at a specific tagged version so we know which version of the context file they were using. Your script seems to be targeting a pre-workshop version of the context, so I created a release tag that I think should replace line 131 of your script:

  "@context" => "https://raw.githubusercontent.com/codemeta/codemeta/0.1-alpha/codemeta.jsonld",

We will create a new tag when the revised version of the codemeta.jsonld schema is finalized. And I could tag another alpha anytime that is useful to you if you want to switch to using the new 'agent' structure. Let me or @gothub know if you want another tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment