2018-05-22 21:43:36 +02:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-08-30 03:24:12 +02:00
|
|
|
|
2017-08-23 01:56:15 +02:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-11-16 16:49:07 +01:00
|
|
|
|
2020-04-14 10:30:25 +02:00
|
|
|
"github.com/pulumi/pulumi/sdk/v2/go/common/workspace"
|
2017-08-23 01:56:15 +02:00
|
|
|
)
|
|
|
|
|
2018-02-14 22:56:16 +01:00
|
|
|
type Projinfo struct {
|
|
|
|
Proj *workspace.Project
|
2017-08-30 03:24:12 +02:00
|
|
|
Root string
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:49:07 +01:00
|
|
|
// GetPwdMain returns the working directory and main entrypoint to use for this package.
|
2018-02-14 22:56:16 +01:00
|
|
|
func (projinfo *Projinfo) GetPwdMain() (string, string, error) {
|
2019-10-10 00:33:35 +02:00
|
|
|
return getPwdMain(projinfo.Root, projinfo.Proj.Main)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PolicyPackInfo struct {
|
|
|
|
Proj *workspace.PolicyPackProject
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPwdMain returns the working directory and main entrypoint to use for this package.
|
|
|
|
func (projinfo *PolicyPackInfo) GetPwdMain() (string, string, error) {
|
|
|
|
return getPwdMain(projinfo.Root, projinfo.Proj.Main)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPwdMain(root, main string) (string, string, error) {
|
|
|
|
pwd := root
|
2018-02-12 01:54:03 +01:00
|
|
|
if main == "" {
|
|
|
|
main = "."
|
|
|
|
} else {
|
2017-11-30 17:14:47 +01:00
|
|
|
|
2021-04-09 06:39:52 +02:00
|
|
|
// The path can be relative from the package root.
|
|
|
|
if !filepath.IsAbs(main) {
|
|
|
|
cleanPwd := filepath.Clean(pwd)
|
|
|
|
main = filepath.Clean(filepath.Join(cleanPwd, main))
|
2017-11-16 16:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// So that any relative paths inside of the program are correct, we still need to pass the pwd
|
|
|
|
// of the main program's parent directory. How we do this depends on if the target is a dir or not.
|
|
|
|
maininfo, err := os.Stat(main)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", errors.Wrapf(err, "project 'main' could not be read")
|
|
|
|
}
|
|
|
|
if maininfo.IsDir() {
|
|
|
|
pwd = main
|
2018-02-12 01:54:03 +01:00
|
|
|
main = "."
|
2017-11-16 16:49:07 +01:00
|
|
|
} else {
|
|
|
|
pwd = filepath.Dir(main)
|
|
|
|
main = filepath.Base(main)
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 01:54:03 +01:00
|
|
|
|
2017-11-16 16:49:07 +01:00
|
|
|
return pwd, main, nil
|
|
|
|
}
|