MCPcopy Index your code
hub / github.com/coder/coder / autoversionFile

Method autoversionFile

scripts/release/main.go:351–432  ·  view source on GitHub ↗
(ctx context.Context, file, channel, version string)

Source from the content-addressed store, hash-verified

349var autoversionMarkdownPragmaRe = regexp.MustCompile(`<!-- ?autoversion\(([^)]+)\): ?"([^"]+)" ?-->`)
350
351func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, version string) error {
352 version = strings.TrimPrefix(version, "v")
353 logger := r.logger.With(slog.F("file", file), slog.F("channel", channel), slog.F("version", version))
354
355 logger.Debug(ctx, "checking file for autoversion pragma")
356
357 contents, err := afero.ReadFile(r.fs, file)
358 if err != nil {
359 return xerrors.Errorf("read file failed: %w", err)
360 }
361
362 lines := strings.Split(string(contents), "\n")
363 var matchRe *regexp.Regexp
364 for i, line := range lines {
365 if autoversionMarkdownPragmaRe.MatchString(line) {
366 matches := autoversionMarkdownPragmaRe.FindStringSubmatch(line)
367 matchChannel := matches[1]
368 match := matches[2]
369
370 logger := logger.With(slog.F("line_number", i+1),
371 slog.F("match_channel", matchChannel), slog.F("match", match))
372
373 logger.Debug(ctx, "autoversion pragma detected")
374
375 if matchChannel != channel {
376 logger.Debug(ctx, "channel mismatch, skipping")
377 continue
378 }
379
380 logger.Info(ctx, "autoversion pragma found with channel match")
381
382 match = strings.Replace(match, "[version]", `(?P<version>[0-9]+\.[0-9]+\.[0-9]+)`, 1)
383 logger.Debug(ctx, "compiling match regexp", slog.F("match", match))
384 matchRe, err = regexp.Compile(match)
385 if err != nil {
386 return xerrors.Errorf("regexp compile failed: %w", err)
387 }
388 }
389 if matchRe != nil {
390 // Apply matchRe and find the group named "version", then replace it
391 // with the new version.
392 if match := matchRe.FindStringSubmatchIndex(line); match != nil {
393 vg := matchRe.SubexpIndex("version")
394 if vg == -1 {
395 logger.Error(ctx, "version group not found in match",
396 slog.F("num_subexp", matchRe.NumSubexp()),
397 slog.F("subexp_names", matchRe.SubexpNames()),
398 slog.F("match", match))
399 return xerrors.Errorf("bug: version group not found in match")
400 }
401 start := match[vg*2]
402 end := match[vg*2+1]
403 logger.Info(ctx, "updating version number", slog.F("line_number", i+1), slog.F("match_start", start), slog.F("match_end", end), slog.F("old_version", line[start:end]))
404 lines[i] = line[:start] + version + line[end:]
405 matchRe = nil
406 break
407 }
408 }

Callers 1

autoversionMethod · 0.95

Calls 7

ReplaceMethod · 0.80
CompileMethod · 0.80
ReadFileMethod · 0.65
WriteFileMethod · 0.65
ErrorfMethod · 0.45
InfoMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected