Merge pull request #54 from yuchenericwu2/singleton_order
context: Changed singletons to execute in order.
diff --git a/context.go b/context.go
index 0109dd3..c5e8b15 100644
--- a/context.go
+++ b/context.go
@@ -69,7 +69,7 @@
moduleGroups map[string]*moduleGroup
moduleInfo map[Module]*moduleInfo
modulesSorted []*moduleInfo
- singletonInfo map[string]*singletonInfo
+ singletonInfo []*singletonInfo
mutatorInfo []*mutatorInfo
earlyMutatorInfo []*earlyMutatorInfo
variantMutatorNames []string
@@ -195,6 +195,7 @@
// set during RegisterSingletonType
factory SingletonFactory
singleton Singleton
+ name string
// set during PrepareBuildActions
actionDefs localBuildActions
@@ -227,7 +228,6 @@
moduleFactories: make(map[string]ModuleFactory),
moduleGroups: make(map[string]*moduleGroup),
moduleInfo: make(map[Module]*moduleInfo),
- singletonInfo: make(map[string]*singletonInfo),
moduleNinjaNames: make(map[string]*moduleGroup),
}
}
@@ -312,20 +312,24 @@
// RegisterSingletonType registers a singleton type that will be invoked to
// generate build actions. Each registered singleton type is instantiated and
-// and invoked exactly once as part of the generate phase.
+// and invoked exactly once as part of the generate phase. Each registered
+// singleton is invoked in registration order.
//
// The singleton type names given here must be unique for the context. The
// factory function should be a named function so that its package and name can
// be included in the generated Ninja file for debugging purposes.
func (c *Context) RegisterSingletonType(name string, factory SingletonFactory) {
- if _, present := c.singletonInfo[name]; present {
- panic(errors.New("singleton name is already registered"))
+ for _, s := range c.singletonInfo {
+ if s.name == name {
+ panic(errors.New("singleton name is already registered"))
+ }
}
- c.singletonInfo[name] = &singletonInfo{
+ c.singletonInfo = append(c.singletonInfo, &singletonInfo{
factory: factory,
singleton: factory(),
- }
+ name: name,
+ })
}
func singletonPkgPath(singleton Singleton) string {
@@ -1706,11 +1710,11 @@
var deps []string
var errs []error
- for name, info := range c.singletonInfo {
+ for _, info := range c.singletonInfo {
// The parent scope of the singletonContext's local scope gets overridden to be that of the
// calling Go package on a per-call basis. Since the initial parent scope doesn't matter we
// just set it to nil.
- scope := newLocalScope(nil, singletonNamespacePrefix(name))
+ scope := newLocalScope(nil, singletonNamespacePrefix(info.name))
sctx := &singletonContext{
context: c,
@@ -2492,15 +2496,7 @@
buf := bytes.NewBuffer(nil)
- singletonNames := make([]string, 0, len(c.singletonInfo))
- for name := range c.singletonInfo {
- singletonNames = append(singletonNames, name)
- }
- sort.Strings(singletonNames)
-
- for _, name := range singletonNames {
- info := c.singletonInfo[name]
-
+ for _, info := range c.singletonInfo {
if len(info.actionDefs.variables)+len(info.actionDefs.rules)+len(info.actionDefs.buildDefs) == 0 {
continue
}
@@ -2512,7 +2508,7 @@
buf.Reset()
infoMap := map[string]interface{}{
- "name": name,
+ "name": info.name,
"goFactory": factoryName,
}
err = headerTemplate.Execute(buf, infoMap)