Beispiel #1
0
// Core returns the core for the given Appfile. The file where the
// Appfile was loaded from should be set in appfile.File.Path. This
// root appfile path will be used as the default output directory
// for Otto.
func (m *Meta) Core(f *appfile.Compiled) (*otto.Core, error) {
	rootDir, err := m.RootDir()
	if err != nil {
		return nil, err
	}

	rootDir, err = filepath.Abs(rootDir)
	if err != nil {
		return nil, err
	}

	dataDir, err := m.DataDir()
	if err != nil {
		return nil, err
	}

	config := *m.CoreConfig
	config.Appfile = f
	config.DataDir = dataDir
	config.LocalDir = filepath.Join(
		rootDir, DefaultOutputDir, DefaultOutputDirLocalData)
	config.CompileDir = filepath.Join(
		rootDir, DefaultOutputDir, DefaultOutputDirCompiledData)
	config.Ui = m.OttoUi()

	config.Directory, err = m.Directory(&config)
	if err != nil {
		return nil, err
	}

	return otto.NewCore(&config)
}
Beispiel #2
0
// Core returns the core for the given Appfile. The file where the
// Appfile was loaded from should be set in appfile.File.Path. This
// root appfile path will be used as the default output directory
// for Otto.
func (m *Meta) Core(f *appfile.Compiled) (*otto.Core, error) {
	if f.File == nil || f.File.Path == "" {
		return nil, fmt.Errorf("Could not determine Appfile dir")
	}

	rootDir, err := m.RootDir(filepath.Dir(f.File.Path))
	if err != nil {
		return nil, err
	}

	rootDir, err = filepath.Abs(rootDir)
	if err != nil {
		return nil, err
	}

	dataDir, err := m.DataDir()
	if err != nil {
		return nil, err
	}

	pluginMgr, err := m.PluginManager()
	if err != nil {
		return nil, err
	}
	if len(pluginMgr.Plugins()) == 0 {
		// We haven't loaded any plugins. Look for them in the
		// used directory and load them.
		usedPath, err := m.AppfilePluginsPath(f)
		if err != nil {
			return nil, err
		}
		if _, err := os.Stat(usedPath); err == nil {
			if err := pluginMgr.LoadUsed(usedPath); err != nil {
				return nil, err
			}
		}
	}

	// Configure the core with what we have from the plugin manager.
	if err := pluginMgr.ConfigureCore(m.CoreConfig); err != nil {
		return nil, err
	}

	config := *m.CoreConfig
	config.Appfile = f
	config.DataDir = dataDir
	config.LocalDir = filepath.Join(
		rootDir, DefaultOutputDir, DefaultOutputDirLocalData)
	config.CompileDir = filepath.Join(
		rootDir, DefaultOutputDir, DefaultOutputDirCompiledData)
	config.Ui = m.OttoUi()

	config.Directory, err = m.Directory(&config)
	if err != nil {
		return nil, err
	}

	return otto.NewCore(&config)
}
Beispiel #3
0
// Core returns the core for the given Appfile. The file where the
// Appfile was loaded from should be set in appfile.File.Path. This
// root appfile path will be used as the default output directory
// for Otto.
func (m *Meta) Core(f *appfile.Compiled) (*otto.Core, error) {
	if f.File == nil || f.File.Path == "" {
		return nil, fmt.Errorf("Could not determine Appfile dir")
	}

	rootDir, err := m.RootDir(filepath.Dir(f.File.Path))
	if err != nil {
		return nil, err
	}

	rootDir, err = filepath.Abs(rootDir)
	if err != nil {
		return nil, err
	}

	dataDir, err := m.DataDir()
	if err != nil {
		return nil, err
	}

	config := *m.CoreConfig
	config.Appfile = f
	config.DataDir = dataDir
	config.LocalDir = filepath.Join(
		rootDir, DefaultOutputDir, DefaultOutputDirLocalData)
	config.CompileDir = filepath.Join(
		rootDir, DefaultOutputDir, DefaultOutputDirCompiledData)
	config.Ui = m.OttoUi()

	config.Directory, err = m.Directory(&config)
	if err != nil {
		return nil, err
	}

	return otto.NewCore(&config)
}
Beispiel #4
0
func (l *Loader) Load(f *appfile.File, dir string) (*appfile.File, error) {
	realFile := f

	// Load the "detected" Appfile
	appDef, err := appfile.Default(dir, l.Detector)
	if err != nil {
		return nil, fmt.Errorf("Error detecting app type: %s", err)
	}

	// Merge the detected Appfile with the real Appfile
	var merged appfile.File
	if err := merged.Merge(appDef); err != nil {
		return nil, fmt.Errorf("Error loading Appfile: %s", err)
	}
	if realFile != nil {
		if err := merged.Merge(realFile); err != nil {
			return nil, fmt.Errorf("Error loading Appfile: %s", err)
		}
	}
	realFile = &merged

	// If we have no application type, there is nothing more to do
	if realFile == nil || realFile.Application.Type == "" {
		return realFile, nil
	}

	// If we're not configured to do any further detection, return it
	if !realFile.Application.Detect {
		return realFile, nil
	}

	// Minimally compile the file that we can use to create a core
	compiled, err := l.Compiler.MinCompile(realFile)
	if err != nil {
		return nil, err
	}

	// Create a temporary directory we use for the core
	td, err := ioutil.TempDir("", "otto")
	if err != nil {
		return nil, err
	}
	defer os.RemoveAll(td)

	// Create a core
	core, err := otto.NewCore(&otto.CoreConfig{
		DataDir:    filepath.Join(td, "data"),
		LocalDir:   filepath.Join(td, "local"),
		CompileDir: filepath.Join(td, "compile"),
		Appfile:    compiled,
		Apps:       l.Apps,
	})
	if err != nil {
		return nil, err
	}

	// Get the app implementation
	appImpl, appCtx, err := core.App()
	if err != nil {
		return nil, err
	}
	defer app.Close(appImpl)

	// Load the implicit Appfile
	implicit, err := appImpl.Implicit(appCtx)
	if err != nil {
		return nil, err
	}

	var final appfile.File
	if err := final.Merge(appDef); err != nil {
		return nil, fmt.Errorf("Error loading Appfile: %s", err)
	}
	if implicit != nil {
		if err := final.Merge(implicit); err != nil {
			return nil, fmt.Errorf("Error loading Appfile: %s", err)
		}
	}
	if f != nil {
		if err := final.Merge(f); err != nil {
			return nil, fmt.Errorf("Error loading Appfile: %s", err)
		}
	}

	return &final, nil
}