(nameVersion string, devDep bool)
| 132 | } |
| 133 | |
| 134 | func (p Port) GenPortTomlString(nameVersion string, devDep bool) (string, error) { |
| 135 | // Checksum from caller affects the result, so include it in the cache key. |
| 136 | key := fmt.Sprintf("%s|%t|%s", nameVersion, devDep, p.Package.Checksum) |
| 137 | if v, ok := portTomlCache.Load(key); ok { |
| 138 | r := v.(metaResult) |
| 139 | return r.meta, r.err |
| 140 | } |
| 141 | |
| 142 | // Store err if init port failed. |
| 143 | var port = Port{DevDep: devDep} |
| 144 | if err := port.Init(p.ctx, nameVersion); err != nil { |
| 145 | portTomlCache.Store(key, metaResult{err: err}) |
| 146 | return "", err |
| 147 | } |
| 148 | |
| 149 | // The build type is one of the key fields to identify a build config. |
| 150 | matchedConfig := port.MatchedConfig |
| 151 | if matchedConfig.BuildType == "" { |
| 152 | matchedConfig.BuildType = p.ctx.BuildType() |
| 153 | } |
| 154 | port.BuildConfigs = []buildsystems.BuildConfig{*matchedConfig} |
| 155 | |
| 156 | // Resolve the source to an immutable value for metadata. |
| 157 | // If the caller's checksum differs from Init (e.g. tampered for cache miss test), use the caller's value. |
| 158 | if p.Package.Checksum != "" { |
| 159 | port.Package.Ref = p.Package.Checksum |
| 160 | } else { |
| 161 | commit, err := port.GetCommitHash(nameVersion, devDep) |
| 162 | if err != nil { |
| 163 | // Don't cache ErrRepoNotExit — the repo may be cloned later. |
| 164 | if !errors.Is(err, errors.ErrRepoNotExit) { |
| 165 | portTomlCache.Store(key, metaResult{err: err}) |
| 166 | } |
| 167 | return "", err |
| 168 | } |
| 169 | if commit != "" { |
| 170 | port.Package.Ref = commit |
| 171 | } |
| 172 | } |
| 173 | port.Package.Checksum = "" |
| 174 | port.Package.Depth = 0 |
| 175 | |
| 176 | // Only export the matched build config for current platform. |
| 177 | bytes, err := toml.Marshal(port) |
| 178 | if err != nil { |
| 179 | portTomlCache.Store(key, metaResult{err: err}) |
| 180 | return "", fmt.Errorf("failed to marshal port %s -> %w", nameVersion, err) |
| 181 | } |
| 182 | result := string(bytes) |
| 183 | portTomlCache.Store(key, metaResult{meta: result}) |
| 184 | return result, nil |
| 185 | } |
| 186 | |
| 187 | // GetCommitHash try to get commit hash from `commitHashCache` first, |
| 188 | // if not exist then get it by calling `doGetCommitHash`. |
no test coverage detected