Files
llgo/xtool/cppkg/cppkg.go

62 lines
1.6 KiB
Go
Raw Normal View History

2025-05-03 23:13:10 +08:00
/*
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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.
*/
package cppkg
import (
"strings"
)
const (
// DefaultFlags is the default flags for package installation.
DefaultFlags = IndexAutoUpdate | ToolQuietInstall
)
2025-05-04 00:08:20 +08:00
// Install installs a package with the given name and version.
2025-05-03 23:13:10 +08:00
// pkgAndVer: 7bitcoder/7bitconf@1.2.0
2025-05-04 00:08:20 +08:00
func Install(pkgAndVer string, flags int) {
2025-05-03 23:13:10 +08:00
pkgPath, ver := parsePkgVer(pkgAndVer)
_, _, err := InstallPkg(pkgPath, ver, nil, flags)
2025-05-03 23:13:10 +08:00
check(err)
}
2025-05-03 23:13:10 +08:00
// InstallPkg installs a package with the given package path and version.
func InstallPkg(pkgPath, ver string, options []string, flags int) (pkg *Package, buildDir string, err error) {
m, err := New("")
if err != nil {
return
}
pkg, err = m.Lookup(pkgPath, ver, flags)
if err == nil {
buildDir, err = m.Install(pkg, options, flags)
}
return
2025-05-03 23:13:10 +08:00
}
func parsePkgVer(pkg string) (string, string) {
parts := strings.SplitN(pkg, "@", 2)
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}
func check(err error) {
if err != nil {
panic(err)
}
}