Skip to content

Commit

Permalink
Generate HTML when (un)yanking a crate
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed May 18, 2024
1 parent 07ed91e commit 3229d24
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 27 deletions.
51 changes: 38 additions & 13 deletions integration-tests/spec/features/crate_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,48 @@
end
end

it 'selects the later version' do
name = 'awesome'
versions = %w[2.0.0 3.0.0 1.0.0]
describe 'the version list' do
it 'selects the later version' do
name = 'awesome'
versions = %w[2.0.0 3.0.0 1.0.0]

versions.each do |version|
scratch
.crate(name:, version:)
.lib_rs(%(pub const ID: &str = "#{version}";))
.publish_to(registry)
versions.each do |version|
scratch
.crate(name:, version:)
.lib_rs(%(pub const ID: &str = "#{version}";))
.publish_to(registry)
end

visit registry.url

aggregate_failures do
within(:section, 'Available crates') do
expect(page).to have_content(name)
expect(page).to have_select('version', with_options: ['1.0.0', '2.0.0', '3.0.0'], selected: '3.0.0')
end
end
end

visit registry.url
it 'does not select yanked versions' do
name = 'awesome'
versions = %w[1.0.0 2.0.0]

aggregate_failures do
within(:section, 'Available crates') do
expect(page).to have_content(name)
expect(page).to have_select('version', with_options: ['1.0.0', '2.0.0', '3.0.0'], selected: '3.0.0')
versions.each do |version|
scratch
.crate(name:, version:)
.lib_rs(%(pub const ID: &str = "#{version}";))
.publish_to(registry)
end

registry.yank(name:, version: '2.0.0')

visit registry.url

aggregate_failures do
within(:section, 'Available crates') do
expect(page).to have_content(name)
expect(page).to have_select('version', with_options: ['1.0.0', '2.0.0 (yanked)'], selected: '1.0.0')
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions integration-tests/spec/support/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def stop
@server.stop
end

def yank(name:, version:)
system(
MARGO_BINARY,
'yank',
'--registry',
@root.to_s,
name,
'--version',
version,
%i[out err] => File::NULL,
exception: true,
)
end

def url
@server.url
end
Expand Down
20 changes: 10 additions & 10 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use indoc::formatdoc;
use maud::{html, Markup, PreEscaped, DOCTYPE};
use semver::Version;
use snafu::prelude::*;
use std::{fs, io, path::PathBuf};

use crate::{ConfigV1, ListAll, Registry};
use crate::{index_entry, ConfigV1, Index, ListAll, Registry};

#[rustfmt::skip]
mod assets;
Expand Down Expand Up @@ -184,8 +185,9 @@ fn index(config: &ConfigV1, crates: &ListAll) -> Markup {
}
td {
select class="w-full" name="version" {
@for (is_last, v) in tag_last(v.keys()) {
option selected[is_last] { (v) }
@for (v, c, select) in most_interesting(v) {
@let suffix = if c.yanked { " (yanked)" } else { "" };
option selected[select] { (v) (suffix) }
}
}
}
Expand All @@ -206,11 +208,9 @@ fn index(config: &ConfigV1, crates: &ListAll) -> Markup {
}
}

fn tag_last<I>(i: I) -> impl Iterator<Item = (bool, I::Item)>
where
I: DoubleEndedIterator,
{
let mut i = i.fuse();
let last = i.next_back().map(|v| (true, v));
i.map(|v| (false, v)).chain(last)
fn most_interesting(i: &Index) -> impl Iterator<Item = (&Version, &index_entry::Root, bool)> {
let last_non_yanked = i.iter().rfind(|(_, c)| !c.yanked).map(|(v, _)| v);

i.iter()
.map(move |(v, c)| (v, c, Some(v) == last_non_yanked))
}
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,9 @@ enum DoInitializeError {

fn do_add(global: &Global, add: AddArgs) -> Result<(), Error> {
let r = Registry::open(&add.registry)?;
r.add(global, &add.path)?;

if r.config.html.enabled {
r.generate_html()?;
}
r.add(global, &add.path)?;
r.maybe_generate_html()?;

Ok(())
}
Expand All @@ -288,7 +286,10 @@ fn do_generate_html(_global: &Global, html: GenerateHtmlArgs) -> Result<(), Erro

fn do_yank(_global: &Global, yank: YankArgs) -> Result<(), Error> {
let r = Registry::open(yank.registry)?;

r.yank(yank.name, yank.version, !yank.undo)?;
r.maybe_generate_html()?;

Ok(())
}

Expand Down Expand Up @@ -415,6 +416,14 @@ impl Registry {
Err(HtmlError)
}

fn maybe_generate_html(&self) -> Result<(), HtmlError> {
if self.config.html.enabled {
self.generate_html()
} else {
Ok(())
}
}

fn yank(&self, name: CrateName, version: Version, yanked: bool) -> Result<(), YankError> {
use yank_error::*;

Expand Down

0 comments on commit 3229d24

Please sign in to comment.