Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/linters.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ This verifies and fixes Flint-managed setup:
- keep lint-managed tool entries under the `# Linters` header
- keep runtime, SDK, and unknown tool entries above that header

With `--fix`, rewrites Flint-managed config in place and advances
`settings.setup_migration_version` when a migration applies.
With `--fix`, rewrites Flint-managed config in place and applies any
currently actionable setup migration.

## `gofmt`

Expand Down
20 changes: 0 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ pub struct Config {
pub struct Settings {
pub base_branch: String,
pub exclude: Vec<String>,
pub setup_migration_version: u32,
}

impl Default for Settings {
fn default() -> Self {
Self {
base_branch: "main".to_string(),
exclude: vec![],
setup_migration_version: crate::setup::V2_BASELINE_SETUP_VERSION,
}
}
}
Expand Down Expand Up @@ -123,21 +121,3 @@ pub fn load(config_dir: &Path) -> Result<Config> {
.extract()?;
Ok(cfg)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn missing_setup_migration_version_defaults_to_v2_baseline() {
let tmp = tempfile::TempDir::new().unwrap();
std::fs::write(tmp.path().join("flint.toml"), "[settings]\n").unwrap();

let cfg = load(tmp.path()).unwrap();

assert_eq!(
cfg.settings.setup_migration_version,
crate::setup::V2_BASELINE_SETUP_VERSION
);
}
}
107 changes: 1 addition & 106 deletions src/init/config_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use crate::registry::EditorconfigDirectiveStyle;

/// Writes a skeleton `flint.toml` in `config_dir`. Creates the directory if needed.
/// Returns `true` if the file was written, `false` if it already existed.
pub(super) fn generate_flint_toml(
config_dir: &Path,
base_branch: &str,
setup_migration_version: u32,
) -> Result<bool> {
pub(super) fn generate_flint_toml(config_dir: &Path, base_branch: &str) -> Result<bool> {
let toml_path = config_dir.join("flint.toml");
if toml_path.exists() {
return Ok(false);
Expand All @@ -21,51 +17,12 @@ pub(super) fn generate_flint_toml(
if base_branch != "main" {
content.push_str(&format!("base_branch = \"{base_branch}\"\n"));
}
content.push_str(&format!(
"setup_migration_version = {setup_migration_version}\n"
));
content.push_str("# exclude = [\"CHANGELOG\\\\.md\"]\n");
std::fs::write(&toml_path, &content)?;
println!(" wrote {}", toml_path.display());
Ok(true)
}

pub(crate) fn write_setup_migration_version(
config_dir: &Path,
base_branch: &str,
version: u32,
) -> Result<bool> {
let toml_path = config_dir.join("flint.toml");
if !toml_path.exists() {
return generate_flint_toml(config_dir, base_branch, version);
}

let content = std::fs::read_to_string(&toml_path)
.with_context(|| format!("failed to read {}", toml_path.display()))?;
let mut doc: toml_edit::DocumentMut = content.parse().context("failed to parse flint.toml")?;
if doc.get("settings").is_none() {
doc["settings"] = toml_edit::table();
}
let Some(settings) = doc.get_mut("settings").and_then(|item| item.as_table_mut()) else {
anyhow::bail!("[settings] is not a table in {}", toml_path.display());
};
let current = settings
.get("setup_migration_version")
.and_then(|item| item.as_value())
.and_then(|value| value.as_integer())
.and_then(|value| u32::try_from(value).ok());
if current == Some(version) {
return Ok(false);
}
settings.insert(
"setup_migration_version",
toml_edit::value(i64::from(version)),
);
std::fs::write(&toml_path, doc.to_string())
.with_context(|| format!("failed to write {}", toml_path.display()))?;
Ok(true)
}

/// Removes stale v1/super-linter-era files that flint v2 no longer uses.
/// Returns the list of removed paths relative to `project_root`.
pub(super) fn remove_legacy_lint_files(
Expand All @@ -90,19 +47,6 @@ pub(super) fn remove_legacy_lint_files(
Ok(removed)
}

pub(super) fn existing_legacy_lint_files(project_root: &Path, config_dir: &Path) -> Vec<String> {
legacy_lint_files(project_root, config_dir)
.into_iter()
.filter(|path| path.exists())
.map(|path| {
path.strip_prefix(project_root)
.unwrap_or(&path)
.display()
.to_string()
})
.collect()
}

fn legacy_lint_files(project_root: &Path, config_dir: &Path) -> Vec<std::path::PathBuf> {
vec![
project_root.join(".prettierignore"),
Expand Down Expand Up @@ -135,16 +79,6 @@ pub(super) fn remove_stale_markdownlint_line_length_directives(
Ok(changed_files)
}

pub(super) fn stale_markdownlint_line_length_directive_files(
project_root: &Path,
) -> Result<Vec<String>> {
stale_transformed_files(
project_root,
&[&["*.md"]],
strip_stale_markdownlint_md013_directives,
)
}

fn tracked_files_for_patterns(project_root: &Path, patterns: &[&[&str]]) -> Result<Vec<String>> {
let mut tracked_files = std::collections::BTreeSet::new();
for group in patterns {
Expand Down Expand Up @@ -197,45 +131,6 @@ pub(super) fn remove_stale_editorconfig_checker_directives(
Ok(changed_files)
}

pub(super) fn stale_editorconfig_checker_directive_files(
project_root: &Path,
delegated_sections: &[(&[&str], EditorconfigDirectiveStyle)],
) -> Result<Vec<String>> {
let mut changed_files = vec![];
for (patterns, directive_style) in delegated_sections {
changed_files.extend(stale_transformed_files(
project_root,
&[*patterns],
|content| strip_stale_editorconfig_checker_directives(content, *directive_style),
)?);
}
changed_files.sort();
changed_files.dedup();
Ok(changed_files)
}

fn stale_transformed_files<F>(
project_root: &Path,
patterns: &[&[&str]],
transform: F,
) -> Result<Vec<String>>
where
F: Fn(&str) -> String,
{
let tracked_files = tracked_files_for_patterns(project_root, patterns)?;
let mut changed_files = vec![];
for rel in tracked_files {
let path = project_root.join(rel.as_str());
let Ok(content) = std::fs::read_to_string(&path) else {
continue;
};
if transform(&content) != content {
changed_files.push(rel);
}
}
Ok(changed_files)
}

fn strip_stale_markdownlint_md013_directives(content: &str) -> String {
let mut kept = Vec::with_capacity(content.lines().count());
let had_trailing_newline = content.ends_with('\n');
Expand Down
Loading
Loading