| 89 | } |
| 90 | |
| 91 | func (p *Port) Init(ctx context.Context, nameVersion string) error { |
| 92 | p.ctx = ctx |
| 93 | p.exprVars = ctx.ExprVars().Clone() |
| 94 | |
| 95 | // Validate name and version. |
| 96 | nameVersion = strings.ReplaceAll(nameVersion, "`", "") |
| 97 | parts := strings.Split(nameVersion, "@") |
| 98 | if len(parts) != 2 { |
| 99 | return fmt.Errorf("port name and version are invalid %s", nameVersion) |
| 100 | } |
| 101 | |
| 102 | // Parse name and version. |
| 103 | p.Name = parts[0] |
| 104 | p.Version = parts[1] |
| 105 | |
| 106 | // Choose the right port file. |
| 107 | portFile, err := p.resolveProjectPort(ctx.Project().GetName(), parts[0], parts[1]) |
| 108 | if err != nil { |
| 109 | if errors.Is(err, errors.ErrPortNotFound) { |
| 110 | if p.Parent != "" { |
| 111 | return fmt.Errorf("%w for %s in %s", errors.ErrPortNotFound, nameVersion, p.Parent) |
| 112 | } |
| 113 | return fmt.Errorf("%w for %s", errors.ErrPortNotFound, nameVersion) |
| 114 | } |
| 115 | return err |
| 116 | } |
| 117 | p.portFile = portFile |
| 118 | |
| 119 | // Decode TOML. |
| 120 | bytes, err := os.ReadFile(p.portFile) |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("failed to read %s -> %w", p.portFile, err) |
| 123 | } |
| 124 | if err := toml.Unmarshal(bytes, p); err != nil { |
| 125 | return fmt.Errorf("failed to unmarshal %s -> %w", p.portFile, err) |
| 126 | } |
| 127 | |
| 128 | // Build_tool ports are always built by native toolchain. |
| 129 | if p.Package.BuildTool { |
| 130 | p.DevDep = true |
| 131 | } |
| 132 | |
| 133 | // Convert build type to lowercase for all build configs. |
| 134 | for index := range p.BuildConfigs { |
| 135 | p.BuildConfigs[index].BuildType = strings.ToLower(p.BuildConfigs[index].BuildType) |
| 136 | p.BuildConfigs[index].BuildType_Windows = strings.ToLower(p.BuildConfigs[index].BuildType_Windows) |
| 137 | p.BuildConfigs[index].BuildType_Linux = strings.ToLower(p.BuildConfigs[index].BuildType_Linux) |
| 138 | p.BuildConfigs[index].BuildType_Darwin = strings.ToLower(p.BuildConfigs[index].BuildType_Darwin) |
| 139 | } |
| 140 | |
| 141 | // Set matchedConfig as prebuilt config when no config found in toml. |
| 142 | matchedConfig, err := p.findMatchedConfig(p.ctx.BuildType()) |
| 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | p.MatchedConfig = matchedConfig |
| 147 | if p.MatchedConfig == nil { |
| 148 | // For build_tool packages not supported on current platform, create a nobuild config. |