• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Package fileutils contains utility functions for files
16package fileutils
17
18import (
19	"path/filepath"
20	"runtime"
21)
22
23// GoSourcePath returns the absolute path to the .go file that calls the
24// function
25func GoSourcePath() string {
26	_, filename, _, ok := runtime.Caller(1)
27	if !ok {
28		panic("No caller information")
29	}
30	path, err := filepath.Abs(filename)
31	if err != nil {
32		panic(err)
33	}
34	return path
35}
36
37// ProjectRoot returns the path to the tint project root
38func ProjectRoot() string {
39	toolRoot := filepath.Dir(GoSourcePath())
40	root, err := filepath.Abs(filepath.Join(toolRoot, "../../.."))
41	if err != nil {
42		panic(err)
43	}
44	return root
45}
46