github.com/goplus/llgo@v0.8.3/internal/mod/mod.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package mod
    18  
    19  import (
    20  	"path"
    21  	"path/filepath"
    22  
    23  	"github.com/goplus/mod"
    24  	"github.com/goplus/mod/gopmod"
    25  )
    26  
    27  // Module represents a Go module.
    28  type Module = gopmod.Module
    29  
    30  // Load loads a Go module from a directory.
    31  func Load(dir string) (ret *Module, pkgPath string, err error) {
    32  	if dir, err = filepath.Abs(dir); err != nil {
    33  		return
    34  	}
    35  	_, gomod, err := mod.FindGoMod(dir)
    36  	if err != nil {
    37  		return
    38  	}
    39  	if ret, err = gopmod.LoadFrom(gomod, ""); err != nil {
    40  		return
    41  	}
    42  	relPath, err := filepath.Rel(ret.Root(), dir)
    43  	if err != nil {
    44  		return
    45  	}
    46  	pkgPath = path.Join(ret.Path(), filepath.ToSlash(relPath))
    47  	return
    48  }