Skip to content

Instantly share code, notes, and snippets.

Last active Feb 20, 2020
What would you like to do?
Let's try and generate some codemeta files.
# For an OO language, this is distinctly procedural. Should probably fix that.
require 'json'
details ={})
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" },
{ :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: 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 ""
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:"
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 ""
when "n", "no"
puts "OK boss..."
puts ""
puts "Reticulating splines"
5.times do
print "."
sleep 0.5
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
# TODO: this is currently a static template (written out here). It would be good
# to do something smarter here.
output = {
"@context" => "",
"@type" => "Code",
"author" => output_authors,
"identifier" => details['doi'],
"codeRepository" => details['url'],
"datePublished" =>"%Y-%m-%d"),
"dateModified" =>"%Y-%m-%d"),
"dateCreated" =>"%Y-%m-%d"),
"description" => details['description'],
"keywords" => details['keywords'],
"license" => details['license'],
"title" => details['title'],
"version" => details['version']
}'codemeta.json', 'w') {|f| f.write(JSON.pretty_generate(output)) }
"@context": "",
"@type": "Code",
"author": [
"@id": "",
"@type": "Person",
"email": "",
"name": "Arfon Smith",
"affiliation": "GitHub Inc."
"identifier": "",
"codeRepository": "",
"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"

This comment has been minimized.

Copy link

@band 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.


This comment has been minimized.

Copy link
Owner Author

@arfon arfon commented Jun 8, 2016

👍 thanks @band.


This comment has been minimized.

Copy link

@mbjones 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" => "",

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