2018-10-20 05:44:13 +09:00
package main
import (
2018-11-29 05:40:11 +09:00
"encoding/json"
"flag"
2018-10-20 05:44:13 +09:00
"fmt"
2018-12-10 05:23:32 +09:00
"github.com/cjslep/activity/tools/exp/convert"
2019-01-12 23:28:21 +09:00
"github.com/cjslep/activity/tools/exp/gen"
2018-12-31 00:54:16 +09:00
"github.com/cjslep/activity/tools/exp/rdf"
2018-11-29 09:22:56 +09:00
"github.com/cjslep/activity/tools/exp/rdf/owl"
"github.com/cjslep/activity/tools/exp/rdf/rdfs"
2019-01-06 20:27:18 +09:00
"github.com/cjslep/activity/tools/exp/rdf/rfc"
2018-11-29 09:22:56 +09:00
"github.com/cjslep/activity/tools/exp/rdf/schema"
"github.com/cjslep/activity/tools/exp/rdf/xsd"
2018-11-29 05:40:11 +09:00
"io/ioutil"
2018-12-10 05:23:32 +09:00
"os"
"strings"
2018-11-29 05:40:11 +09:00
)
var registry * rdf . RDFRegistry
func mustAddOntology ( o rdf . Ontology ) {
if registry == nil {
registry = rdf . NewRDFRegistry ( )
}
if err := registry . AddOntology ( o ) ; err != nil {
panic ( err )
}
}
func init ( ) {
2018-12-10 05:23:32 +09:00
mustAddOntology ( & xsd . XMLOntology { Package : "xml" } )
2018-11-29 05:40:11 +09:00
mustAddOntology ( & owl . OWLOntology { } )
2018-12-10 05:23:32 +09:00
mustAddOntology ( & rdf . RDFOntology { Package : "rdf" } )
2018-11-29 05:40:11 +09:00
mustAddOntology ( & rdfs . RDFSchemaOntology { } )
mustAddOntology ( & schema . SchemaOntology { } )
2019-01-06 20:27:18 +09:00
mustAddOntology ( & rfc . RFCOntology { Package : "rfc" } )
2018-11-29 05:40:11 +09:00
}
2018-12-10 05:23:32 +09:00
type list [ ] string
func ( l * list ) String ( ) string {
return strings . Join ( * l , "," )
}
func ( l * list ) Set ( v string ) error {
vals := strings . Split ( v , "," )
* l = append ( * l , vals ... )
return nil
}
2019-01-17 05:43:36 +09:00
type CommandLineFlags struct {
specs list
prefix * string
individual * bool
}
2018-11-29 05:40:11 +09:00
2019-01-17 05:43:36 +09:00
func NewCommandLineFlags ( ) * CommandLineFlags {
c := & CommandLineFlags {
// TODO: Be more rigorous when applying this. Also, clear the default value I am using for convenience.
prefix : flag . String ( "prefix" , "github.com/cjslep/activity/tools/exp/tmp" , "Package prefix to use for all generated package paths. This should be the prefix in the GOPATH directory if generating in a subdirectory." ) ,
individual : flag . Bool ( "individual" , false , "Whether to generate types and properties in individual packages." ) ,
2018-11-29 05:40:11 +09:00
}
2019-01-17 05:43:36 +09:00
flag . Var ( & ( c . specs ) , "spec" , "Input JSON-LD specification used to generate Go code." )
flag . Parse ( )
if err := c . validate ( ) ; err != nil {
2018-11-29 05:40:11 +09:00
panic ( err )
}
2019-01-17 05:43:36 +09:00
return c
}
func ( c * CommandLineFlags ) validate ( ) error {
if len ( c . specs ) == 0 {
return fmt . Errorf ( "specs must not be empty" )
}
return nil
}
func main ( ) {
cmd := NewCommandLineFlags ( )
inputJSONs := make ( [ ] rdf . JSONLD , 0 , len ( cmd . specs ) )
for _ , spec := range cmd . specs {
b , err := ioutil . ReadFile ( spec )
if err != nil {
panic ( err )
}
var inputJSON map [ string ] interface { }
err = json . Unmarshal ( b , & inputJSON )
if err != nil {
panic ( err )
}
inputJSONs = append ( inputJSONs , inputJSON )
}
p , err := rdf . ParseVocabularies ( registry , inputJSONs )
2018-11-04 00:56:09 +09:00
if err != nil {
panic ( err )
}
2019-01-12 22:37:37 +09:00
policy := convert . FlatUnderRoot
2019-01-17 05:43:36 +09:00
if * cmd . individual {
2019-01-12 22:37:37 +09:00
policy = convert . IndividualUnderRoot
}
2018-12-10 05:23:32 +09:00
c := & convert . Converter {
2019-01-17 05:43:36 +09:00
Registry : registry ,
GenRoot : gen . NewPackageManager ( * cmd . prefix , "gen" ) ,
PackagePolicy : policy ,
2018-12-10 05:23:32 +09:00
}
f , err := c . Convert ( p )
if err != nil {
panic ( err )
}
for _ , file := range f {
if e := os . MkdirAll ( "./" + file . Directory , 0777 ) ; e != nil {
panic ( e )
}
2018-12-31 00:54:16 +09:00
if e := file . F . Save ( "./" + file . Directory + "/" + file . FileName ) ; e != nil {
2018-12-10 05:23:32 +09:00
panic ( e )
}
}
2019-01-17 05:43:36 +09:00
fmt . Printf ( "Done\n" )
2018-10-20 05:44:13 +09:00
}