Skip to content

Commit

Permalink
Merge pull request #8883 from Turbo87/install-snippet
Browse files Browse the repository at this point in the history
Show `cargo install` for crates with binaries
  • Loading branch information
Turbo87 authored Jun 27, 2024
2 parents 86dedf5 + ff0405a commit 0bd0c11
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 97 deletions.
38 changes: 7 additions & 31 deletions app/components/crate-sidebar.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,13 @@
<div data-test-install>
<h2 local-class="heading">Install</h2>

<p local-class="copy-help">Run the following Cargo command in your project directory:</p>
{{#if (is-clipboard-supported)}}
<CopyButton
@copyText={{this.cargoAddCommand}}
title="Copy command to clipboard"
local-class="copy-button"
>
<span local-class="selectable">{{this.cargoAddCommand}}</span>
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
</CopyButton>
{{else}}
<code local-class="copy-fallback">
{{this.cargoAddCommand}}
</code>
{{/if}}

<p local-class="copy-help">Or add the following line to your Cargo.toml:</p>
{{#if (is-clipboard-supported)}}
<CopyButton
@copyText={{this.tomlSnippet}}
title="Copy Cargo.toml snippet to clipboard"
local-class="copy-button"
>
<span local-class="selectable">{{this.tomlSnippet}}</span>
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
</CopyButton>
{{else}}
<code local-class="copy-fallback">
{{this.tomlSnippet}}
</code>
{{/if}}
<CrateSidebar::InstallInstructions
@crate={{@crate.name}}
@version={{@version.num}}
@exactVersion={{@requestedVersion}}
@hasLib={{not (@version.has_lib false)}}
@binNames={{@version.bin_names}}
/>
</div>
{{/unless}}

Expand Down
12 changes: 0 additions & 12 deletions app/components/crate-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ export default class CrateSidebar extends Component {
return homepage && (!repository || simplifyUrl(repository) !== simplifyUrl(homepage));
}

get cargoAddCommand() {
return this.args.requestedVersion
? `cargo add ${this.args.crate.name}@=${this.args.requestedVersion}`
: `cargo add ${this.args.crate.name}`;
}

get tomlSnippet() {
let version = this.args.version.num.split('+')[0];
let exact = this.args.requestedVersion ? '=' : '';
return `${this.args.crate.name} = "${exact}${version}"`;
}

get playgroundLink() {
let playgroundCrates = this.playground.crates;
if (!playgroundCrates) return;
Expand Down
54 changes: 0 additions & 54 deletions app/components/crate-sidebar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,60 +50,6 @@
font-variant-numeric: tabular-nums;
}

.copy-help {
font-size: 12px;
}

.copy-button,
.copy-fallback {
display: flex;
width: 100%;
align-items: center;
justify-content: space-between;
padding: var(--space-2xs) var(--space-xs);
font-family: var(--font-monospace);
font-size: 14px;
line-height: 1.5em;
color: var(--main-color);
background: transparent;
border-radius: var(--space-3xs);
border: solid var(--space-4xs) var(--gray-border);

span {
flex: auto;
display: block;
word-break: break-word;
}
}

.copy-button {
text-align: start;
cursor: pointer;

&:hover {
background-color: light-dark(white, #141413);
}
}

.copy-icon {
flex-shrink: 0;
height: 1.1em;
width: auto;
/* for slightly nicer alignment... */
margin-top: -3px;
margin-left: var(--space-2xs);
opacity: 0;
transition: opacity var(--transition-fast);

.copy-button:hover & {
opacity: 1;
}
}

.selectable {
user-select: text;
}

.links {
> * + * {
margin-top: var(--space-m);
Expand Down
82 changes: 82 additions & 0 deletions app/components/crate-sidebar/install-instructions.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{{#if @binNames}}
{{#if (is-clipboard-supported)}}
<CopyButton
@copyText={{this.cargoInstallCommand}}
title="Copy command to clipboard"
local-class="copy-button"
>
<span local-class="selectable">{{this.cargoInstallCommand}}</span>
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
</CopyButton>
{{else}}
<code local-class="copy-fallback">
{{this.cargoInstallCommand}}
</code>
{{/if}}

<p local-class="copy-help">
{{#if (eq @binNames.length 1)}}
Running the above command will globally install the
<span local-class="bin-name">{{get @binNames 0}}</span>
binary.
{{else if (eq @binNames.length 2)}}
Running the above command will globally install the
<span local-class="bin-name">{{get @binNames 0}}</span>
and
<span local-class="bin-name">{{get @binNames 1}}</span>
binaries.
{{else}}
Running the above command will globally install these binaries:
{{#each @binNames as |binName index|~}}
{{~#if (eq index 0)~}}
<span local-class="bin-name">{{binName}}</span>
{{~else if (eq index (sum @binNames.length -1))}}
and <span local-class="bin-name">{{binName}}</span>
{{~else~}}
, <span local-class="bin-name">{{binName}}</span>
{{~/if}}
{{~/each}}
{{/if}}
</p>

{{/if}}

{{#if (and @hasLib @binNames)}}
<h3>Install as library</h3>
{{/if}}

{{#if @hasLib}}
<p local-class="copy-help">Run the following Cargo command in your project directory:</p>

{{#if (is-clipboard-supported)}}
<CopyButton
@copyText={{this.cargoAddCommand}}
title="Copy command to clipboard"
local-class="copy-button"
>
<span local-class="selectable">{{this.cargoAddCommand}}</span>
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
</CopyButton>
{{else}}
<code local-class="copy-fallback">
{{this.cargoAddCommand}}
</code>
{{/if}}

<p local-class="copy-help">Or add the following line to your Cargo.toml:</p>

{{#if (is-clipboard-supported)}}
<CopyButton
@copyText={{this.tomlSnippet}}
title="Copy Cargo.toml snippet to clipboard"
local-class="copy-button"
>
<span local-class="selectable">{{this.tomlSnippet}}</span>
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
</CopyButton>
{{else}}
<code local-class="copy-fallback">
{{this.tomlSnippet}}
</code>
{{/if}}
{{/if}}
21 changes: 21 additions & 0 deletions app/components/crate-sidebar/install-instructions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Component from '@glimmer/component';

export default class InstallInstructions extends Component {
get cargoInstallCommand() {
return this.args.exactVersion
? `cargo install ${this.args.crate}@${this.args.version}`
: `cargo install ${this.args.crate}`;
}

get cargoAddCommand() {
return this.args.exactVersion
? `cargo add ${this.args.crate}@=${this.args.version}`
: `cargo add ${this.args.crate}`;
}

get tomlSnippet() {
let version = this.args.version.split('+')[0];
let exact = this.args.exactVersion ? '=' : '';
return `${this.args.crate} = "${exact}${version}"`;
}
}
59 changes: 59 additions & 0 deletions app/components/crate-sidebar/install-instructions.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.copy-help {
font-size: 12px;
overflow-wrap: break-word;
}

.copy-button,
.copy-fallback {
display: flex;
width: 100%;
align-items: center;
justify-content: space-between;
padding: var(--space-2xs) var(--space-xs);
font-family: var(--font-monospace);
font-size: 14px;
line-height: 1.5em;
color: var(--main-color);
background: transparent;
border-radius: var(--space-3xs);
border: solid var(--space-4xs) var(--gray-border);

span {
flex: auto;
display: block;
word-break: break-word;
}
}

.copy-button {
text-align: start;
cursor: pointer;

&:hover {
background-color: light-dark(white, #141413);
}
}

.copy-icon {
flex-shrink: 0;
height: 1.1em;
width: auto;
/* for slightly nicer alignment... */
margin-top: -3px;
margin-left: var(--space-2xs);
opacity: 0;
transition: opacity var(--transition-fast);

.copy-button:hover & {
opacity: 1;
}
}

.selectable {
user-select: text;
}

.bin-name {
font-family: var(--font-monospace);
font-weight: bold;
}
5 changes: 5 additions & 0 deletions app/helpers/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { helper } from '@ember/component/helper';

export default helper(function ([...values]) {
return values.reduce((a, b) => a + b, 0);
});
5 changes: 5 additions & 0 deletions app/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default class Version extends Model {
*/
@attr rust_version;

/** @type {boolean | null} */
@attr has_lib;
/** @type {string[] | null} */
@attr bin_names;

@belongsTo('crate', { async: false, inverse: 'versions' }) crate;

@belongsTo('user', { async: false, inverse: null }) published_by;
Expand Down

0 comments on commit 0bd0c11

Please sign in to comment.