activity/tools/exp/main.go

97 行
2.6 KiB
Go
Raw 通常表示 履歴

2018-10-20 05:44:13 +09:00
package main
import (
"encoding/json"
"flag"
2018-10-20 05:44:13 +09:00
"fmt"
"github.com/cjslep/activity/tools/exp/convert"
"github.com/cjslep/activity/tools/exp/props"
"github.com/cjslep/activity/tools/exp/rdf"
"github.com/cjslep/activity/tools/exp/rdf/owl"
"github.com/cjslep/activity/tools/exp/rdf/rdfs"
"github.com/cjslep/activity/tools/exp/rdf/schema"
"github.com/cjslep/activity/tools/exp/rdf/xsd"
"io/ioutil"
"os"
"strings"
)
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() {
mustAddOntology(&xsd.XMLOntology{Package: "xml"})
mustAddOntology(&owl.OWLOntology{})
mustAddOntology(&rdf.RDFOntology{Package: "rdf"})
mustAddOntology(&rdfs.RDFSchemaOntology{})
mustAddOntology(&schema.SchemaOntology{})
}
var (
input = flag.String("input", "spec.json", "Input JSON-LD specification used to generate Go code.")
vocabName = flag.String("vocab", "ActivityStreams", "The vocabulary name being generated by 'input'.")
// 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.")
2018-10-20 05:44:13 +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() {
flag.Parse()
// TODO: Flag validation
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)
}
p, err := rdf.ParseVocabulary(*vocabName, registry, inputJSON)
if err != nil {
panic(err)
}
c := &convert.Converter{
Registry: registry,
VocabularyRoot: props.NewPackageManager(*prefix, "gen/as"),
ValueRoot: props.NewPackageManager(*prefix, "gen/vals"),
PropertyPackagePolicy: convert.PropertyFlatUnderRoot,
PropertyPackageRoot: props.NewPackageManager(*prefix, "gen/as/props"),
TypePackagePolicy: convert.TypeFlatUnderRoot,
TypePackageRoot: props.NewPackageManager(*prefix, "gen/as/types"),
}
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)
}
if e := file.F.Save("./" + file.Directory + "/" + file.FileName); e != nil {
panic(e)
}
}
fmt.Printf("done")
2018-10-20 05:44:13 +09:00
}