clippier 0.2.0

MoosicBox clippier package
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
//! Cargo workspace publishing support.
//!
//! This module publishes Cargo workspace crates in dependency order while skipping
//! versions that already exist on crates.io. Development dependencies are ignored
//! for ordering so dev-dependency cycles do not block publication.

use std::{
    collections::{BTreeMap, BTreeSet},
    fs,
    io::Write as _,
    path::{Path, PathBuf},
    process::{Command, Stdio},
    thread,
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

use cargo_metadata::{DependencyKind as CargoDependencyKind, MetadataCommand, PackageId};
use serde::{Deserialize, Serialize};

use crate::OutputType;

type BoxError = Box<dyn std::error::Error + Send + Sync>;

const CRATES_IO_API: &str = "https://crates.io/api/v1";

/// Configuration for publishing a Cargo workspace.
#[derive(Debug, Clone)]
pub struct PublishConfig {
    /// Path to the workspace root or workspace `Cargo.toml`.
    pub workspace_root: PathBuf,
    /// Specific packages to publish. Normal/build workspace dependencies are included automatically.
    pub packages: Option<Vec<String>>,
    /// Compute and print the publish plan without running `cargo publish`.
    pub dry_run: bool,
    /// Run Cargo's local verification step.
    pub verify: bool,
    /// Pass `--allow-dirty` to `cargo publish`.
    pub allow_dirty: bool,
    /// Maximum time to wait for each newly published crate version to appear on crates.io.
    pub publish_timeout: Duration,
    /// Delay between crates.io availability checks.
    pub publish_poll_interval: Duration,
}

impl Default for PublishConfig {
    fn default() -> Self {
        Self {
            workspace_root: PathBuf::from("."),
            packages: None,
            dry_run: false,
            verify: false,
            allow_dirty: false,
            publish_timeout: Duration::from_mins(5),
            publish_poll_interval: Duration::from_secs(10),
        }
    }
}

/// Publish status for a single workspace package.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PublishPackageStatus {
    /// The package manifest disables crates.io publishing.
    PublishDisabled,
    /// The same package version already exists on crates.io.
    AlreadyPublished,
    /// The package would be published, but `--dry-run` was used.
    DryRun,
    /// The package was published successfully.
    Published,
}

/// A single package entry in a publish report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublishPackageReport {
    /// Package name.
    pub name: String,
    /// Package version.
    pub version: String,
    /// Resulting status.
    pub status: PublishPackageStatus,
}

impl PublishPackageReport {
    #[must_use]
    fn new(
        name: impl Into<String>,
        version: impl Into<String>,
        status: PublishPackageStatus,
    ) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            status,
        }
    }
}

/// Summary produced by `clippier publish`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublishReport {
    /// Packages in the order they were considered.
    pub packages: Vec<PublishPackageReport>,
}

impl PublishReport {
    #[must_use]
    const fn new(packages: Vec<PublishPackageReport>) -> Self {
        Self { packages }
    }

    #[must_use]
    fn count(&self, status: PublishPackageStatus) -> usize {
        self.packages
            .iter()
            .filter(|package| package.status == status)
            .count()
    }

    #[must_use]
    fn to_raw_string(&self) -> String {
        let mut lines = Vec::new();

        if self.packages.is_empty() {
            lines.push("No workspace packages matched the publish request".to_string());
        } else {
            lines.push("Publish results:".to_string());
            for package in &self.packages {
                let label = match package.status {
                    PublishPackageStatus::PublishDisabled => "skip publish=false",
                    PublishPackageStatus::AlreadyPublished => "skip already published",
                    PublishPackageStatus::DryRun => "dry-run publish",
                    PublishPackageStatus::Published => "published",
                };
                lines.push(format!("  {label}: {}@{}", package.name, package.version));
            }
        }

        lines.push(String::new());
        lines.push("Summary:".to_string());
        lines.push(format!(
            "  publish disabled: {}",
            self.count(PublishPackageStatus::PublishDisabled)
        ));
        lines.push(format!(
            "  already published: {}",
            self.count(PublishPackageStatus::AlreadyPublished)
        ));
        lines.push(format!(
            "  dry-run: {}",
            self.count(PublishPackageStatus::DryRun)
        ));
        lines.push(format!(
            "  published: {}",
            self.count(PublishPackageStatus::Published)
        ));

        lines.join("\n")
    }
}

/// Internal package data needed for publish planning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublishPackageInfo {
    /// Package name.
    pub name: String,
    /// Package version.
    pub version: String,
    /// Whether the package can be published to crates.io.
    pub publishable: bool,
    /// Workspace dependencies that must exist before this package can be published.
    pub publish_dependencies: BTreeSet<String>,
    /// Package directory containing `Cargo.toml`.
    pub package_path: Option<PathBuf>,
}

impl PublishPackageInfo {
    /// Creates package info for publish planning.
    #[must_use]
    pub fn new(
        name: impl Into<String>,
        version: impl Into<String>,
        publishable: bool,
        publish_dependencies: BTreeSet<String>,
    ) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            publishable,
            publish_dependencies,
            package_path: None,
        }
    }

    #[must_use]
    fn with_package_path(mut self, package_path: PathBuf) -> Self {
        self.package_path = Some(package_path);
        self
    }
}

/// A deterministic publish plan.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublishPlan {
    /// Packages that are not publishable to crates.io.
    pub publish_disabled: Vec<String>,
    /// Publishable package order, dependencies first.
    pub publish_order: Vec<String>,
}

/// Build a publish plan from package metadata.
///
/// Development dependencies are ignored. Normal and build dependencies are ordered before dependents.
///
/// # Errors
///
/// * If a requested package does not exist
/// * If a normal/build workspace dependency cycle is detected
/// * If a requested package depends on an unknown workspace package
pub fn build_publish_plan(
    packages: &BTreeMap<String, PublishPackageInfo>,
    requested_packages: Option<&[String]>,
) -> Result<PublishPlan, BoxError> {
    let requested = collect_requested_packages(packages, requested_packages)?;
    let mut publish_disabled = Vec::new();
    let mut publishable_requested = BTreeSet::new();

    for name in &requested {
        let package = packages
            .get(name)
            .ok_or_else(|| format!("Unknown workspace package '{name}'"))?;
        if package.publishable {
            publishable_requested.insert(name.clone());
        } else {
            publish_disabled.push(name.clone());
        }
    }

    let mut temporary = BTreeSet::new();
    let mut permanent = BTreeSet::new();
    let mut publish_order = Vec::new();

    for name in &publishable_requested {
        visit_publish_package(
            name,
            packages,
            &publishable_requested,
            &mut temporary,
            &mut permanent,
            &mut publish_order,
        )?;
    }

    Ok(PublishPlan {
        publish_disabled,
        publish_order,
    })
}

fn collect_requested_packages(
    packages: &BTreeMap<String, PublishPackageInfo>,
    requested_packages: Option<&[String]>,
) -> Result<BTreeSet<String>, BoxError> {
    let mut requested = BTreeSet::new();

    if let Some(package_names) = requested_packages {
        for name in package_names {
            if !packages.contains_key(name) {
                return Err(format!("Unknown workspace package '{name}'").into());
            }
            collect_with_publish_dependencies(name, packages, &mut requested)?;
        }
    } else {
        requested.extend(packages.keys().cloned());
    }

    Ok(requested)
}

fn collect_with_publish_dependencies(
    name: &str,
    packages: &BTreeMap<String, PublishPackageInfo>,
    requested: &mut BTreeSet<String>,
) -> Result<(), BoxError> {
    if !requested.insert(name.to_string()) {
        return Ok(());
    }

    let package = packages
        .get(name)
        .ok_or_else(|| format!("Unknown workspace package '{name}'"))?;

    for dependency in &package.publish_dependencies {
        collect_with_publish_dependencies(dependency, packages, requested)?;
    }

    Ok(())
}

fn visit_publish_package(
    name: &str,
    packages: &BTreeMap<String, PublishPackageInfo>,
    publishable_requested: &BTreeSet<String>,
    temporary: &mut BTreeSet<String>,
    permanent: &mut BTreeSet<String>,
    publish_order: &mut Vec<String>,
) -> Result<(), BoxError> {
    if permanent.contains(name) {
        return Ok(());
    }
    if !temporary.insert(name.to_string()) {
        return Err(format!(
            "Normal/build workspace dependency cycle detected while planning publication at '{name}'"
        )
        .into());
    }

    let package = packages
        .get(name)
        .ok_or_else(|| format!("Unknown workspace package '{name}'"))?;

    for dependency in &package.publish_dependencies {
        let dependency_package = packages
            .get(dependency)
            .ok_or_else(|| format!("Unknown workspace package '{dependency}'"))?;

        if !dependency_package.publishable {
            continue;
        }

        if publishable_requested.contains(dependency) {
            visit_publish_package(
                dependency,
                packages,
                publishable_requested,
                temporary,
                permanent,
                publish_order,
            )?;
        }
    }

    temporary.remove(name);
    permanent.insert(name.to_string());
    publish_order.push(name.to_string());
    Ok(())
}

/// Handles the `publish` command.
///
/// # Errors
///
/// * If workspace metadata cannot be loaded
/// * If a requested package does not exist
/// * If a publishable package has an unpublished non-publishable workspace dependency
/// * If crates.io cannot be queried
/// * If `cargo publish` fails
#[allow(clippy::too_many_lines)]
pub async fn handle_publish_command(
    config: PublishConfig,
    output: OutputType,
) -> Result<String, BoxError> {
    let workspace_root = normalize_workspace_root(&config.workspace_root);
    eprintln!(
        "Loading workspace metadata from {}",
        workspace_root.display()
    );
    let packages = load_publish_packages(&workspace_root)?;
    let client = crates_io_client()?;

    let initial_plan = build_publish_plan(&packages, config.packages.as_deref())?;
    eprintln!(
        "Planned {} publishable package(s), {} publish-disabled package(s)",
        initial_plan.publish_order.len(),
        initial_plan.publish_disabled.len()
    );
    let mut reports = Vec::new();
    let mut already_published = BTreeSet::new();
    let mut needs_publish = BTreeSet::new();

    for name in &initial_plan.publish_disabled {
        if let Some(package) = packages.get(name) {
            eprintln!(
                "Skipping {}@{} (publish disabled)",
                package.name, package.version
            );
            reports.push(PublishPackageReport::new(
                &package.name,
                &package.version,
                PublishPackageStatus::PublishDisabled,
            ));
        }
    }

    for name in &initial_plan.publish_order {
        let package = packages
            .get(name)
            .ok_or_else(|| format!("Unknown workspace package '{name}'"))?;

        eprintln!("Checking {}@{} on crates.io", package.name, package.version);
        if crate_version_exists(&client, &package.name, &package.version).await? {
            eprintln!(
                "Skipping {}@{} (already published)",
                package.name, package.version
            );
            already_published.insert(name.clone());
            reports.push(PublishPackageReport::new(
                &package.name,
                &package.version,
                PublishPackageStatus::AlreadyPublished,
            ));
        } else {
            needs_publish.insert(name.clone());
        }
    }

    validate_publish_dependencies_available(&client, &packages, &needs_publish, &already_published)
        .await?;

    let packages_to_publish = filter_packages_to_publish(&packages, &needs_publish);
    let final_plan = build_publish_plan(&packages_to_publish, None)?;

    for name in final_plan.publish_order {
        let package = packages
            .get(&name)
            .ok_or_else(|| format!("Unknown workspace package '{name}'"))?;

        if config.dry_run {
            eprintln!("Would publish {}@{}", package.name, package.version);
            reports.push(PublishPackageReport::new(
                &package.name,
                &package.version,
                PublishPackageStatus::DryRun,
            ));
            continue;
        }

        eprintln!("Publishing {}@{}", package.name, package.version);
        match publish_package(&workspace_root, &packages, package, &config)? {
            PublishAttempt::AlreadyPublished => {
                eprintln!(
                    "Skipping {}@{} (already published)",
                    package.name, package.version
                );
                reports.push(PublishPackageReport::new(
                    &package.name,
                    &package.version,
                    PublishPackageStatus::AlreadyPublished,
                ));
            }
            PublishAttempt::Published => {
                wait_for_crate_version(
                    &client,
                    &package.name,
                    &package.version,
                    config.publish_timeout,
                    config.publish_poll_interval,
                )
                .await?;

                eprintln!("Published {}@{}", package.name, package.version);
                reports.push(PublishPackageReport::new(
                    &package.name,
                    &package.version,
                    PublishPackageStatus::Published,
                ));
            }
        }
    }

    let report = PublishReport::new(reports);
    match output {
        OutputType::Raw => Ok(report.to_raw_string()),
        OutputType::Json => Ok(serde_json::to_string_pretty(&report)?),
    }
}

fn normalize_workspace_root(path: &Path) -> PathBuf {
    if path.file_name().is_some_and(|name| name == "Cargo.toml") {
        path.parent()
            .map_or_else(|| PathBuf::from("."), Path::to_path_buf)
    } else {
        path.to_path_buf()
    }
}

fn load_publish_packages(
    workspace_root: &Path,
) -> Result<BTreeMap<String, PublishPackageInfo>, BoxError> {
    let mut command = MetadataCommand::new();
    command.current_dir(workspace_root).no_deps();
    let metadata = command.exec()?;

    let workspace_members = metadata
        .workspace_members
        .iter()
        .cloned()
        .collect::<BTreeSet<PackageId>>();

    let workspace_package_names = metadata
        .packages
        .iter()
        .filter(|package| workspace_members.contains(&package.id))
        .map(|package| package.name.to_string())
        .collect::<BTreeSet<_>>();

    let mut packages = BTreeMap::new();

    for package in metadata
        .packages
        .iter()
        .filter(|package| workspace_members.contains(&package.id))
    {
        let publish_dependencies = package
            .dependencies
            .iter()
            .filter(|dependency| {
                matches!(
                    dependency.kind,
                    CargoDependencyKind::Normal | CargoDependencyKind::Build
                ) && workspace_package_names.contains(dependency.name.as_str())
            })
            .map(|dependency| dependency.name.clone())
            .collect::<BTreeSet<_>>();

        let publishable = package
            .publish
            .as_ref()
            .is_none_or(|registries| registries.iter().any(|registry| registry == "crates-io"));

        let manifest_path = package.manifest_path.clone().into_std_path_buf();
        let package_path = manifest_path
            .parent()
            .map_or_else(|| workspace_root.to_path_buf(), Path::to_path_buf);

        packages.insert(
            package.name.to_string(),
            PublishPackageInfo::new(
                package.name.to_string(),
                package.version.to_string(),
                publishable,
                publish_dependencies,
            )
            .with_package_path(package_path),
        );
    }

    Ok(packages)
}

fn filter_packages_to_publish(
    packages: &BTreeMap<String, PublishPackageInfo>,
    needs_publish: &BTreeSet<String>,
) -> BTreeMap<String, PublishPackageInfo> {
    packages
        .iter()
        .filter(|(name, _package)| needs_publish.contains(*name))
        .map(|(name, package)| {
            let mut package = package.clone();
            package
                .publish_dependencies
                .retain(|dependency| needs_publish.contains(dependency));
            (name.clone(), package)
        })
        .collect()
}

async fn validate_publish_dependencies_available(
    client: &reqwest::Client,
    packages: &BTreeMap<String, PublishPackageInfo>,
    needs_publish: &BTreeSet<String>,
    already_published: &BTreeSet<String>,
) -> Result<(), BoxError> {
    for name in needs_publish {
        let package = packages
            .get(name)
            .ok_or_else(|| format!("Unknown workspace package '{name}'"))?;

        for dependency in &package.publish_dependencies {
            if needs_publish.contains(dependency) || already_published.contains(dependency) {
                continue;
            }

            let dependency_package = packages
                .get(dependency)
                .ok_or_else(|| format!("Unknown workspace package '{dependency}'"))?;

            if crate_version_exists(
                client,
                &dependency_package.name,
                &dependency_package.version,
            )
            .await?
            {
                continue;
            }

            return Err(format!(
                "Package '{name}' depends on '{dependency}@{}', but that version is not published and is not publishable in this workspace",
                dependency_package.version
            )
            .into());
        }
    }

    Ok(())
}

fn crates_io_client() -> Result<reqwest::Client, BoxError> {
    Ok(reqwest::Client::builder()
        .user_agent(concat!(
            "clippier/",
            env!("CARGO_PKG_VERSION"),
            " (https://github.com/MoosicBox/MoosicBox)"
        ))
        .build()?)
}

async fn crate_version_exists(
    client: &reqwest::Client,
    name: &str,
    version: &str,
) -> Result<bool, BoxError> {
    let response = client
        .get(format!("{CRATES_IO_API}/crates/{name}/{version}"))
        .send()
        .await?;

    if response.status().is_success() {
        Ok(true)
    } else if response.status() == reqwest::StatusCode::NOT_FOUND {
        Ok(false)
    } else {
        Err(format!(
            "Failed to query crates.io for {name}@{version}: HTTP {}",
            response.status()
        )
        .into())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PublishAttempt {
    AlreadyPublished,
    Published,
}

fn publish_package(
    workspace_root: &Path,
    packages: &BTreeMap<String, PublishPackageInfo>,
    package: &PublishPackageInfo,
    config: &PublishConfig,
) -> Result<PublishAttempt, BoxError> {
    let sanitized = SanitizedPackage::prepare(workspace_root, packages, package)?;

    let mut command = Command::new("cargo");
    command
        .arg("publish")
        .arg("--manifest-path")
        .arg(sanitized.manifest_path())
        .current_dir(sanitized.path());

    if !config.verify {
        command.arg("--no-verify");
    }
    if config.allow_dirty {
        command.arg("--allow-dirty");
    }

    let output = run_streaming_command(command)?;
    if output.status.success() {
        return Ok(PublishAttempt::Published);
    }

    if output.stderr.contains("already exists") {
        return Ok(PublishAttempt::AlreadyPublished);
    }

    Err(format!(
        "cargo publish failed for {}@{} with status {} (cargo output was streamed above)",
        package.name, package.version, output.status
    )
    .into())
}

#[derive(Debug)]
struct CommandOutput {
    status: std::process::ExitStatus,
    stderr: String,
}

fn run_streaming_command(mut command: Command) -> Result<CommandOutput, BoxError> {
    command.stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut child = command.spawn()?;

    let stdout = child
        .stdout
        .take()
        .ok_or("Failed to capture cargo publish stdout")?;
    let stderr = child
        .stderr
        .take()
        .ok_or("Failed to capture cargo publish stderr")?;

    let stdout_handle = thread::spawn(move || stream_output(stdout, false));
    let stderr_handle = thread::spawn(move || stream_output(stderr, true));

    let status = child.wait()?;
    stdout_handle
        .join()
        .map_err(|_| "Failed to join stdout streaming thread")??;
    let stderr = stderr_handle
        .join()
        .map_err(|_| "Failed to join stderr streaming thread")??;

    Ok(CommandOutput { status, stderr })
}

fn stream_output(mut reader: impl std::io::Read, stderr: bool) -> Result<String, std::io::Error> {
    let mut bytes = Vec::new();
    let mut buffer = [0; 8192];

    loop {
        let read = reader.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        bytes.extend_from_slice(&buffer[..read]);
        if stderr {
            std::io::stderr().write_all(&buffer[..read])?;
            std::io::stderr().flush()?;
        } else {
            std::io::stdout().write_all(&buffer[..read])?;
            std::io::stdout().flush()?;
        }
    }

    Ok(String::from_utf8_lossy(&bytes).into_owned())
}

#[derive(Debug)]
struct SanitizedPackage {
    path: PathBuf,
}

impl SanitizedPackage {
    fn prepare(
        workspace_root: &Path,
        packages: &BTreeMap<String, PublishPackageInfo>,
        package: &PublishPackageInfo,
    ) -> Result<Self, BoxError> {
        let package_path = package
            .package_path
            .as_ref()
            .ok_or_else(|| format!("Missing package path for {}", package.name))?;
        let path = std::env::temp_dir().join(format!(
            "clippier-publish-{}-{}-{}",
            package.name,
            std::process::id(),
            SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos()
        ));

        copy_dir(package_path, &path)?;
        sanitize_manifest(workspace_root, packages, &path.join("Cargo.toml"))?;

        Ok(Self { path })
    }

    fn path(&self) -> &Path {
        &self.path
    }

    fn manifest_path(&self) -> PathBuf {
        self.path.join("Cargo.toml")
    }
}

impl Drop for SanitizedPackage {
    fn drop(&mut self) {
        if let Err(error) = fs::remove_dir_all(&self.path) {
            log::warn!(
                "Failed to remove sanitized publish directory {}: {error}",
                self.path.display()
            );
        }
    }
}

fn copy_dir(from: &Path, to: &Path) -> Result<(), BoxError> {
    fs::create_dir_all(to)?;

    for entry in fs::read_dir(from)? {
        let entry = entry?;
        let source = entry.path();
        let destination = to.join(entry.file_name());
        let file_type = entry.file_type()?;

        if file_type.is_dir() {
            copy_dir(&source, &destination)?;
        } else if file_type.is_file() {
            fs::copy(&source, &destination)?;
        } else if file_type.is_symlink() {
            let target = fs::read_link(&source)?;
            create_symlink(&target, &destination)?;
        }
    }

    Ok(())
}

#[cfg(unix)]
fn create_symlink(target: &Path, destination: &Path) -> Result<(), BoxError> {
    std::os::unix::fs::symlink(target, destination)?;
    Ok(())
}

#[cfg(windows)]
fn create_symlink(target: &Path, destination: &Path) -> Result<(), BoxError> {
    if target.is_dir() {
        std::os::windows::fs::symlink_dir(target, destination)?;
    } else {
        std::os::windows::fs::symlink_file(target, destination)?;
    }
    Ok(())
}

fn sanitize_manifest(
    workspace_root: &Path,
    packages: &BTreeMap<String, PublishPackageInfo>,
    manifest_path: &Path,
) -> Result<(), BoxError> {
    let workspace_toml = read_toml(&workspace_root.join("Cargo.toml"))?;
    let mut manifest = read_toml(manifest_path)?;
    let package_versions = packages
        .iter()
        .map(|(name, package)| (name.clone(), package.version.clone()))
        .collect::<BTreeMap<_, _>>();

    resolve_workspace_package_fields(&mut manifest, &workspace_toml)?;
    sanitize_dependency_sections(&mut manifest, &workspace_toml, &package_versions)?;
    if let Some(table) = manifest.as_table_mut() {
        table.remove("workspace");
    }

    fs::write(manifest_path, toml::to_string_pretty(&manifest)?)?;
    Ok(())
}

fn read_toml(path: &Path) -> Result<toml::Value, BoxError> {
    Ok(toml::from_str(&fs::read_to_string(path)?)?)
}

fn resolve_workspace_package_fields(
    manifest: &mut toml::Value,
    workspace_toml: &toml::Value,
) -> Result<(), BoxError> {
    let Some(package_table) = manifest
        .get_mut("package")
        .and_then(toml::Value::as_table_mut)
    else {
        return Ok(());
    };
    let workspace_package = workspace_toml
        .get("workspace")
        .and_then(|workspace| workspace.get("package"))
        .and_then(toml::Value::as_table);

    let keys = package_table.keys().cloned().collect::<Vec<_>>();
    for key in keys {
        if !is_workspace_inherited(package_table.get(&key)) {
            continue;
        }

        let value = workspace_package
            .and_then(|workspace_package| workspace_package.get(&key))
            .ok_or_else(|| format!("Missing workspace.package.{key}"))?
            .clone();
        package_table.insert(key, value);
    }

    Ok(())
}

fn sanitize_dependency_sections(
    manifest: &mut toml::Value,
    workspace_toml: &toml::Value,
    package_versions: &BTreeMap<String, String>,
) -> Result<(), BoxError> {
    sanitize_dependency_table(manifest, "dependencies", workspace_toml, package_versions)?;
    sanitize_dependency_table(
        manifest,
        "build-dependencies",
        workspace_toml,
        package_versions,
    )?;
    remove_table_key(manifest, "dev-dependencies");

    if let Some(targets) = manifest
        .get_mut("target")
        .and_then(toml::Value::as_table_mut)
    {
        for (_target_name, target) in targets.iter_mut() {
            sanitize_dependency_table(target, "dependencies", workspace_toml, package_versions)?;
            sanitize_dependency_table(
                target,
                "build-dependencies",
                workspace_toml,
                package_versions,
            )?;
            remove_table_key(target, "dev-dependencies");
        }
    }

    Ok(())
}

fn sanitize_dependency_table(
    manifest: &mut toml::Value,
    section: &str,
    workspace_toml: &toml::Value,
    package_versions: &BTreeMap<String, String>,
) -> Result<(), BoxError> {
    let Some(dependencies) = manifest
        .get_mut(section)
        .and_then(toml::Value::as_table_mut)
    else {
        return Ok(());
    };

    let keys = dependencies.keys().cloned().collect::<Vec<_>>();
    for key in keys {
        let Some(value) = dependencies.get_mut(&key) else {
            continue;
        };

        if is_workspace_inherited(Some(value)) {
            *value = resolve_workspace_dependency(&key, value, workspace_toml)?;
        }

        strip_workspace_path_dependency(&key, value, package_versions);
    }

    Ok(())
}

fn resolve_workspace_dependency(
    key: &str,
    value: &toml::Value,
    workspace_toml: &toml::Value,
) -> Result<toml::Value, BoxError> {
    let workspace_dependencies = workspace_toml
        .get("workspace")
        .and_then(|workspace| workspace.get("dependencies"))
        .and_then(toml::Value::as_table)
        .ok_or("Missing workspace.dependencies")?;
    let workspace_dependency = workspace_dependencies
        .get(key)
        .ok_or_else(|| format!("Missing workspace dependency '{key}'"))?;

    let Some(local_table) = value.as_table() else {
        return Ok(workspace_dependency.clone());
    };

    let mut resolved = match workspace_dependency {
        toml::Value::Table(table) => table.clone(),
        toml::Value::String(version) => {
            let mut table = toml::map::Map::new();
            table.insert("version".to_string(), toml::Value::String(version.clone()));
            table
        }
        other => return Ok(other.clone()),
    };

    for (local_key, local_value) in local_table {
        if local_key != "workspace" {
            resolved.insert(local_key.clone(), local_value.clone());
        }
    }

    Ok(toml::Value::Table(resolved))
}

fn strip_workspace_path_dependency(
    key: &str,
    value: &mut toml::Value,
    package_versions: &BTreeMap<String, String>,
) {
    let toml::Value::Table(table) = value else {
        return;
    };

    let package_name = table
        .get("package")
        .and_then(toml::Value::as_str)
        .unwrap_or(key);

    let Some(version) = package_versions.get(package_name) else {
        return;
    };

    table.remove("path");
    table.remove("workspace");
    table
        .entry("version".to_string())
        .or_insert_with(|| toml::Value::String(version.clone()));
}

fn remove_table_key(value: &mut toml::Value, key: &str) {
    if let Some(table) = value.as_table_mut() {
        table.remove(key);
    }
}

fn is_workspace_inherited(value: Option<&toml::Value>) -> bool {
    value
        .and_then(toml::Value::as_table)
        .and_then(|table| table.get("workspace"))
        .and_then(toml::Value::as_bool)
        .unwrap_or(false)
}

async fn wait_for_crate_version(
    client: &reqwest::Client,
    name: &str,
    version: &str,
    timeout: Duration,
    poll_interval: Duration,
) -> Result<(), BoxError> {
    let started = Instant::now();

    while started.elapsed() <= timeout {
        if crate_version_exists(client, name, version).await? {
            return Ok(());
        }
        std::thread::sleep(poll_interval);
    }

    Err(format!(
        "Timed out waiting for {name}@{version} to appear on crates.io after {} seconds",
        timeout.as_secs()
    )
    .into())
}

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

    fn package(
        name: &str,
        publishable: bool,
        dependencies: impl IntoIterator<Item = &'static str>,
    ) -> PublishPackageInfo {
        PublishPackageInfo::new(
            name,
            "0.1.0",
            publishable,
            dependencies
                .into_iter()
                .map(ToString::to_string)
                .collect::<BTreeSet<_>>(),
        )
    }

    #[test]
    fn publish_plan_orders_dependencies_before_dependents() {
        let packages = BTreeMap::from([
            ("app".to_string(), package("app", true, ["core"])),
            ("core".to_string(), package("core", true, ["models"])),
            ("models".to_string(), package("models", true, [])),
        ]);

        let plan = build_publish_plan(&packages, None).unwrap();

        assert_eq!(plan.publish_order, ["models", "core", "app"]);
    }

    #[test]
    fn publish_plan_includes_transitive_dependencies_for_requested_package() {
        let packages = BTreeMap::from([
            ("app".to_string(), package("app", true, ["core"])),
            ("core".to_string(), package("core", true, ["models"])),
            ("models".to_string(), package("models", true, [])),
            ("unrelated".to_string(), package("unrelated", true, [])),
        ]);

        let plan = build_publish_plan(&packages, Some(&["app".to_string()])).unwrap();

        assert_eq!(plan.publish_order, ["models", "core", "app"]);
    }

    #[test]
    fn publish_plan_rejects_normal_dependency_cycle() {
        let packages = BTreeMap::from([
            ("a".to_string(), package("a", true, ["b"])),
            ("b".to_string(), package("b", true, ["a"])),
        ]);

        let error = build_publish_plan(&packages, None).unwrap_err().to_string();

        assert!(error.contains("cycle"));
    }

    #[test]
    fn publish_plan_skips_publish_disabled_packages() {
        let packages = BTreeMap::from([
            ("publishable".to_string(), package("publishable", true, [])),
            ("private".to_string(), package("private", false, [])),
        ]);

        let plan = build_publish_plan(&packages, None).unwrap();

        assert_eq!(plan.publish_disabled, ["private"]);
        assert_eq!(plan.publish_order, ["publishable"]);
    }

    #[test]
    fn publish_plan_skips_private_dependencies_for_publish_order() {
        let packages = BTreeMap::from([
            ("app".to_string(), package("app", true, ["private"])),
            ("private".to_string(), package("private", false, [])),
        ]);

        let plan = build_publish_plan(&packages, None).unwrap();

        assert_eq!(plan.publish_disabled, ["private"]);
        assert_eq!(plan.publish_order, ["app"]);
    }

    #[test]
    fn sanitize_manifest_strips_dev_dependencies_and_resolves_workspace_values() {
        let root = std::env::temp_dir().join(format!(
            "clippier-publish-test-{}-{}",
            std::process::id(),
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let package_dir = root.join("packages/app");
        fs::create_dir_all(&package_dir).unwrap();

        fs::write(
            root.join("Cargo.toml"),
            r#"
                [workspace]
                members = ["packages/app", "packages/models", "packages/test-utils"]

                [workspace.package]
                edition = "2024"
                license = "MPL-2.0"
                repository = "https://example.com/repo"
                version = "1.2.3"

                [workspace.dependencies]
                models = { version = "1.2.3", path = "packages/models", default-features = false }
                test-utils = { version = "1.2.3", path = "packages/test-utils" }
                serde = "1.0.228"
            "#,
        )
        .unwrap();

        fs::write(
            package_dir.join("Cargo.toml"),
            r#"
                [package]
                edition = { workspace = true }
                license = { workspace = true }
                name = "app"
                repository = { workspace = true }
                version = { workspace = true }

                [dependencies]
                models = { workspace = true, features = ["api"] }
                serde = { workspace = true, features = ["derive"] }

                [dev-dependencies]
                test-utils = { workspace = true }

                [target.'cfg(unix)'.dev-dependencies]
                test-utils = { workspace = true }
            "#,
        )
        .unwrap();

        let packages = BTreeMap::from([
            ("app".to_string(), package("app", true, ["models"])),
            ("models".to_string(), package("models", true, [])),
            ("test-utils".to_string(), package("test-utils", true, [])),
        ]);

        sanitize_manifest(&root, &packages, &package_dir.join("Cargo.toml")).unwrap();
        let sanitized = fs::read_to_string(package_dir.join("Cargo.toml")).unwrap();
        let sanitized_toml: toml::Value = toml::from_str(&sanitized).unwrap();
        let package = sanitized_toml.get("package").unwrap();
        let dependencies = sanitized_toml.get("dependencies").unwrap();
        let models = dependencies.get("models").unwrap();
        let serde = dependencies.get("serde").unwrap();

        assert_eq!(package.get("edition").unwrap().as_str(), Some("2024"));
        assert_eq!(package.get("version").unwrap().as_str(), Some("1.2.3"));
        assert!(sanitized_toml.get("dev-dependencies").is_none());
        assert!(
            sanitized_toml
                .get("target")
                .unwrap()
                .get("cfg(unix)")
                .unwrap()
                .get("dev-dependencies")
                .is_none()
        );
        assert!(models.get("path").is_none());
        assert_eq!(models.get("version").unwrap().as_str(), Some("1.2.3"));
        assert_eq!(serde.get("version").unwrap().as_str(), Some("1.0.228"));

        fs::remove_dir_all(root).unwrap();
    }
}