1// Copyright 2014 Google Inc. All rights reserved.
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
15package pathtools
16
17import (
18	"path/filepath"
19	"strings"
20)
21
22// PrefixPaths returns a list of paths consisting of prefix joined with each
23// element of paths.  The resulting paths are "clean" in the filepath.Clean
24// sense.
25func PrefixPaths(paths []string, prefix string) []string {
26	result := make([]string, len(paths))
27	for i, path := range paths {
28		result[i] = filepath.Join(prefix, path)
29	}
30	return result
31}
32
33func ReplaceExtensions(paths []string, extension string) []string {
34	result := make([]string, len(paths))
35	for i, path := range paths {
36		result[i] = ReplaceExtension(path, extension)
37	}
38	return result
39}
40
41// ReplaceExtension changes the file extension. If the file does not have an
42// extension, the new extension is appended.
43func ReplaceExtension(path string, extension string) string {
44	oldExt := filepath.Ext(path)
45	if oldExt != "" {
46		path = strings.TrimSuffix(path, oldExt)
47	}
48	return path + "." + extension
49}
50