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
}
var (
2019-01-15 06:06:03 +09:00
input = flag . String ( "input" , "spec.json" , "Input JSON-LD specification used to generate Go code." )
2019-01-03 08:29:24 +09:00
// TODO: Be more rigorous when applying this. Also, clear the default value I am using for convenience.
2019-01-12 22:37:37 +09:00
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-10-20 05:44:13 +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
}
2018-10-20 05:44:13 +09:00
func main ( ) {
2018-11-29 05:40:11 +09:00
flag . Parse ( )
2018-12-10 05:23:32 +09:00
// TODO: Flag validation
2018-11-29 05:40:11 +09:00
b , err := ioutil . ReadFile ( * input )
if err != nil {
panic ( err )
}
var inputJSON map [ string ] interface { }
err = json . Unmarshal ( b , & inputJSON )
if err != nil {
panic ( err )
}
2019-01-15 06:06:03 +09:00
p , err := rdf . ParseVocabulary ( registry , inputJSON )
2018-11-04 00:56:09 +09:00
if err != nil {
panic ( err )
}
2019-01-12 22:37:37 +09:00
policy := convert . FlatUnderRoot
if * individual {
policy = convert . IndividualUnderRoot
}
2019-01-15 06:06:03 +09:00
fmt . Printf ( "Vocab Name: %q\n" , p . Vocab . Name )
2018-12-10 05:23:32 +09:00
c := & convert . Converter {
2019-01-07 03:44:24 +09:00
Registry : registry ,
2019-01-12 23:28:21 +09:00
GenRoot : gen . NewPackageManager ( * prefix , "gen" ) ,
2019-01-15 06:06:03 +09:00
VocabularyName : p . Vocab . Name ,
2019-01-12 22:37:37 +09:00
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-14 06:34:29 +09:00
fmt . Printf ( "done\n" )
2018-10-20 05:44:13 +09:00
}