1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-29 23:01:16 +03:00
This commit is contained in:
Niels
2015-07-26 10:59:17 +02:00
parent eb782770de
commit c8ced4ecbc
3 changed files with 2 additions and 2 deletions

221
doc/scripts/git-update-ghpages Executable file
View File

@ -0,0 +1,221 @@
#!/usr/bin/env ruby
# git update-ghpages user/repo -b gh-pages -p manual/ -i
require 'fileutils'
require 'tmpdir'
module Params
def extract(what) i = index(what) and slice!(i, 2)[1] end;
def first_is(what) shift if what.include?(self.first); end
def self.[](*what) what.extend Params; end
def ===(argv) argv.first_is(self); end
end
# ============================================================================
ARGV.extend Params
class CLI
# CLI options
attr_reader :prefix #=> "doc/"
attr_reader :input #=> "/home/me/projects/foo"
attr_reader :message #=> "Updated"
attr_reader :repo #=> "git@github.com:me/project.git"
attr_reader :url #=> "http://me.github.com/project"
attr_reader :branch #=> "gh-pages"
def verbose?() @verbose; end
def force?() @force; end
def simulate?() @simulate; end
def initialize
# Switches
@verbose = !! (ARGV.extract('--verbose') || ARGV.delete('-v'))
@simulate = !! (ARGV.extract('--simulate') || ARGV.delete('-s'))
@force = !! (ARGV.delete('--force') || ARGV.delete('-f'))
# Stuff
@prefix = ARGV.extract('--prefix') || ARGV.extract('-p') || ''
@input = File.expand_path(ARGV.extract('--input') || ARGV.extract('-i') || '.')
@message = ARGV.extract('--message') || ARGV.extract('-m') || 'Update'
# Github info
branch = ARGV.extract('--branch') || ARGV.extract('-b') || nil
@repo, @url, @branch = get_github_info(ARGV.shift, branch)
end
def git_current_branch
`git rev-parse --abbrev-ref HEAD`.strip
end
def git_deploy
in_temp_path do |temppath|
status "Cloning repository"
system! "git clone #{repo} -b #{branch} #{temppath}"
if git_current_branch != branch
status "Warning: No #{branch} branch found in repo, creating one."
return git_deploy_force
end
copy_files input, File.join(temppath, prefix)
status "Committing files"
system! "git add .; git add -u; git commit -m #{message.to_s.inspect}"
unless simulate?
status "Updating repo"
system! "git push origin #{branch}"
end
true
end
end
def git_deploy_force
in_temp_path do |temppath|
status "Creating new repository"
system! "git init ."
system! "git checkout -b gh-pages"
copy_files input, File.join(temppath, prefix)
status "Committing files"
system! "git add . && git commit -m #{message.to_s.inspect}"
unless simulate?
status "Updating repo"
system! "git push #{repo} gh-pages:#{branch} --force"
end
true
end
end
def get_github_info(repo, branch=nil, prefix=nil)
if github_format?(repo)
user, repo_name = repo.split('/')
r = "git@github.com:#{repo}.git"
# User page or project page?
if repo_name =~ /\.github\.com/
[r, "http://#{repo_name}/#{prefix}", branch || 'master' ]
else
[r, "http://#{user}.github.com/#{repo_name}/#{prefix}", branch || 'gh-pages' ]
end
else
[repo, nil, branch]
end
end
def run!
unless repo
print_help
exit 128
end
status "Deploying to #{repo} (branch #{branch})"
msg "NOTE: Running in simulation mode." if simulate?
msg "WARNING: If the repository has gh-pages history, it with be overriden." if force? && !simulate?
result = force? ? git_deploy_force : git_deploy
if result
puts ""
status "Done."
msg "See: #{url}" if url && !simulate?
else
tip "Failed."
exit 1
end
end
def status(str)
puts "#{c('===>',34)} #{c(str, 32)}"
end
def msg(str)
puts " #{c(str, 32)}"
end
def c(str, color)
"\033[#{color}m#{str}\033[0m"
end
def print_help
tip \
%{Usage: git update-ghpages username/repository [options]
Flags:
-f, --force Force an update (WARNING: kills the history!)
-s, --simulate Creates the repository, but doesn't push.
-v, --verbose Verbose mode
Options:
-p PATH, --prefix The prefix
-i PATH, --input Input (defaults to current directory)
-b BRANCH, --branch The branch to deploy to (defaults to gh-pages)
-m MSG, --message Commit message (defaults to 'Update')
Examples:
Update the repo 'coffee' of github user 'james' with the files from the
current directory. The files will be in http://james.github.com/coffee.
$ git update-ghpages james/coffee
Same as above, but take the files from 'doc/'.
$ git update-ghpages james/coffee -i doc
Same as the first, but the files will instead be in
http://james.github.com/coffee/manual.
$ git update-ghpages james/coffee -i doc -p manual
}.gsub(/^ {4}/, '')
end
private # Helpers
def tip(msg)
$stderr.write "#{msg}\n"
end
def github_format?(str)
str =~ /^([A-Za-z0-9\-_]+)\/([A-Za-z0-9\-_\.]+)$/
end
# Performs actions inside a temp path.
def in_temp_path(&blk)
require 'tmpdir'
Dir.mktmpdir do |dir|
Dir.chdir(dir) { yield dir }
end
end
def system!(str)
puts `#{str} 2>&1`.strip.gsub(/^/, " ")
raise "Failed with exit code #{$?.to_i}" unless $?.to_i == 0
end
# Returns the current branch name
def git_branch
`git symbolic-ref HEAD`.strip.split('/').last
end
# Copy files from source folder to another
def copy_files(from, to)
status "Copying files #{from} => #{to}..." if verbose?
Dir["#{from}/**/*"].each do |f|
next unless File.file?(f)
target = File.join(to, f.gsub(/^#{Regexp.escape from}/, ''))
FileUtils.mkdir_p File.dirname(target)
msg "%20s => %-20s" % [f, target] if verbose?
FileUtils.cp f, target
end
end
end
CLI.new.run!

119
doc/scripts/send_to_wandbox.py Executable file
View File

@ -0,0 +1,119 @@
#! /usr/bin/env python
# This script uploads a directory to Wandbox (http://melpon.org/wandbox),
# which is an online compiler environment, and prints a permalink to the
# uploaded code. We use this to provide a "Try it online" version of the
# library to make the barrier to entry as low as possible.
#
# This script was adapted from the script proposed in
# https://github.com/melpon/wandbox/issues/153.
#
# To know how to use this script: ./wandbox.py --help
#
# Copyright Louis Dionne 2015
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
import argparse
import fnmatch
import json
import os
import re
import urllib2
# Strips C and C++ comments from the given string.
#
# Copied from http://stackoverflow.com/a/241506/627587.
def strip_comments(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
# Post the given JSON data to Wandbox's API, and return the result
# as a JSON object.
def upload(options):
request = urllib2.Request('http://melpon.org/wandbox/api/compile.json')
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request, json.dumps(options))
return json.loads(response.read())
# Returns a list of the '.hpp' headers in the given directory and in
# subdirectories.
#
# The path must be absolute, and the returned paths are all absolute too.
def headers(path):
return [
os.path.join(dir, file)
for (dir, _, files) in os.walk(path)
for file in fnmatch.filter(files, "*.hpp")
]
def main():
parser = argparse.ArgumentParser(description=
"""Upload a directory to Wandbox (http://melpon.org/wandbox).
On success, the program prints a permalink to the uploaded
directory on Wandbox and returns 0. On error, it prints the
response from the Wandbox API and returns 1.
Note that the comments are stripped from all the headers in the
uploaded directory.
"""
)
parser.add_argument('directory', type=str, help=
"""A directory to upload to Wandbox.
The path may be either absolute or relative to the current directory.
However, the names of the files uploaded to Wandbox will all be
relative to this directory. This way, one can easily specify the
directory to be '/some/project/include', and the uploaded files
will be uploaded as-if they were rooted at '/some/project/include'
""")
parser.add_argument('main', type=str, help=
"""The main source file.
The path may be either absolute or relative to the current directory.
"""
)
args = parser.parse_args()
directory = os.path.abspath(args.directory)
if not os.path.exists(directory):
raise Exception("'%s' is not a valid directory" % args.directory)
cpp = os.path.abspath(args.main)
if not os.path.exists(cpp):
raise Exception("'%s' is not a valid file name" % args.main)
response = upload({
'code': open(cpp).read(),
'codes': [{
'file': os.path.relpath(header, directory),
'code': strip_comments(open(header).read())
} for header in headers(directory)],
'options': 'boost-nothing,c++11',
'compiler': 'gcc-4.9.2',
'save': True,
'compiler-option-raw': '-I.'
})
if 'status' in response and response['status'] == '0':
print response['url']
return 0
else:
print response
return 1
exit(main())