@@ -11,15 +11,22 @@ require 'logger'
1111require 'json/ld'
1212require 'getoptlong'
1313require 'open-uri'
14+ require 'ruby-prof'
1415
1516def run ( input , options )
1617 if options [ :profile ]
1718 output_dir = File . expand_path ( "../../doc/profiles/#{ File . basename __FILE__ , ".rb" } " , __FILE__ )
1819 FileUtils . mkdir_p ( output_dir )
19- result = RubyProf . profile do
20- run ( input , profile : false , **options )
21- end
22- result . eliminate_methods! ( [ /Hash#each/ , /JSON::LD::Utils#debug/ , /Array#map/ , /JSON::LD::Utils#depth/ ] )
20+ profile = RubyProf ::Profile . new
21+ profile . exclude_methods! ( Array , :each , :map )
22+ profile . exclude_method! ( Hash , :each )
23+ profile . exclude_method! ( Kernel , :require )
24+ profile . exclude_method! ( Object , :run )
25+ profile . exclude_common_methods!
26+ profile . start
27+ run ( input , **options . merge ( profile : false ) )
28+ result = profile . stop
29+
2330 # Print a graph profile to text
2431 printer = RubyProf ::MultiPrinter . new ( result )
2532 printer . print ( path : output_dir , profile : "profile" )
@@ -29,7 +36,6 @@ def run(input, options)
2936 reader_class = RDF ::Reader . for ( options [ :input_format ] . to_sym )
3037 raise "Reader not found for #{ options [ :input_format ] } " unless reader_class
3138
32- prefixes = { }
3339 start = Time . new
3440 if options [ :flatten ]
3541 output = JSON ::LD ::API . flatten ( input , options . delete ( :context ) , **options )
@@ -95,25 +101,42 @@ options = {
95101}
96102input = nil
97103
98- opts = GetoptLong . new (
99- [ "--debug" , GetoptLong ::NO_ARGUMENT ] ,
100- [ "--automatic" , GetoptLong ::NO_ARGUMENT ] ,
101- [ "--compact" , GetoptLong ::NO_ARGUMENT ] ,
102- [ "--context" , GetoptLong ::REQUIRED_ARGUMENT ] ,
103- [ "--execute" , "-e" , GetoptLong ::REQUIRED_ARGUMENT ] ,
104- [ "--expand" , GetoptLong ::NO_ARGUMENT ] ,
105- [ "--expanded" , GetoptLong ::NO_ARGUMENT ] ,
106- [ "--flatten" , GetoptLong ::NO_ARGUMENT ] ,
107- [ "--format" , GetoptLong ::REQUIRED_ARGUMENT ] ,
108- [ "--frame" , GetoptLong ::REQUIRED_ARGUMENT ] ,
109- [ "--input-format" , GetoptLong ::REQUIRED_ARGUMENT ] ,
110- [ "--output" , "-o" , GetoptLong ::REQUIRED_ARGUMENT ] ,
111- [ "--profile" , GetoptLong ::NO_ARGUMENT ] ,
112- [ "--parse-only" , GetoptLong ::NO_ARGUMENT ] ,
113- [ "--quiet" , GetoptLong ::NO_ARGUMENT ] ,
114- [ "--uri" , GetoptLong ::REQUIRED_ARGUMENT ] ,
115- [ "--validate" , GetoptLong ::NO_ARGUMENT ]
116- )
104+ OPT_ARGS = [
105+ [ "--debug" , GetoptLong ::NO_ARGUMENT , "Debug output" ] ,
106+ [ "--compact" , GetoptLong ::NO_ARGUMENT , "Compact input, using context" ] ,
107+ [ "--context" , GetoptLong ::REQUIRED_ARGUMENT , "Context used for compaction" ] ,
108+ [ "--execute" , "-e" , GetoptLong ::REQUIRED_ARGUMENT , "Use option argument as the patch input" ] ,
109+ [ "--expand" , GetoptLong ::NO_ARGUMENT , "Expand input" ] ,
110+ [ "--expanded" , GetoptLong ::NO_ARGUMENT , "Input is already expanded" ] ,
111+ [ "--flatten" , GetoptLong ::NO_ARGUMENT , "Flatten input" ] ,
112+ [ "--format" , GetoptLong ::REQUIRED_ARGUMENT , "Output format, for RDF output" ] ,
113+ [ "--frame" , GetoptLong ::REQUIRED_ARGUMENT , "Frame input, option value is frame to use" ] ,
114+ [ "--help" , "-?" , GetoptLong ::NO_ARGUMENT , "This message" ] ,
115+ [ "--input-format" , GetoptLong ::REQUIRED_ARGUMENT , "Format of input, if not JSON-LD" ] ,
116+ [ "--output" , "-o" , GetoptLong ::REQUIRED_ARGUMENT , "Where to store output (default STDOUT)" ] ,
117+ [ "--profile" , GetoptLong ::NO_ARGUMENT , "Run profiler with output to doc/profiles/" ] ,
118+ [ "--quiet" , GetoptLong ::NO_ARGUMENT , "Reduce output" ] ,
119+ [ "--uri" , GetoptLong ::REQUIRED_ARGUMENT , "Run with argument value as base" ] ,
120+ [ "--validate" , GetoptLong ::NO_ARGUMENT , "Validate input" ] ,
121+ ]
122+
123+ opts = GetoptLong . new ( *OPT_ARGS . map { |o | o [ 0 ..-2 ] } )
124+
125+ def usage
126+ STDERR . puts %{Usage: #{ $0} [options] file ...}
127+ width = OPT_ARGS . map do |o |
128+ l = o . first . length
129+ l += o [ 1 ] . length + 2 if o [ 1 ] . is_a? ( String )
130+ l
131+ end . max
132+ OPT_ARGS . each do |o |
133+ s = " %-*s " % [ width , ( o [ 1 ] . is_a? ( String ) ? "#{ o [ 0 , 2 ] . join ( ', ' ) } " : o [ 0 ] ) ]
134+ s += o . last
135+ STDERR . puts s
136+ end
137+ exit ( 1 )
138+ end
139+
117140opts . each do |opt , arg |
118141 case opt
119142 when '--debug' then logger . level = Logger ::DEBUG
@@ -125,9 +148,9 @@ opts.each do |opt, arg|
125148 when '--flatten' then options [ :flatten ] = true
126149 when '--format' then options [ :output_format ] = arg . to_sym
127150 when '--frame' then options [ :frame ] = arg
151+ when "--help" then usage
128152 when '--input-format' then options [ :input_format ] = arg . to_sym
129153 when '--output' then options [ :output ] = File . open ( arg , "w" )
130- when '--parse-only' then options [ :parse_only ] = true
131154 when '--profile' then options [ :profile ] = true
132155 when '--quiet'
133156 options [ :quiet ] = true
0 commit comments