Skip to content

Commit 0bd0c11

Browse files
authored
Merge pull request #8883 from Turbo87/install-snippet
Show `cargo install` for crates with binaries
2 parents 86dedf5 + ff0405a commit 0bd0c11

File tree

8 files changed

+179
-97
lines changed

8 files changed

+179
-97
lines changed

app/components/crate-sidebar.hbs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,13 @@
4848
<div data-test-install>
4949
<h2 local-class="heading">Install</h2>
5050

51-
<p local-class="copy-help">Run the following Cargo command in your project directory:</p>
52-
{{#if (is-clipboard-supported)}}
53-
<CopyButton
54-
@copyText={{this.cargoAddCommand}}
55-
title="Copy command to clipboard"
56-
local-class="copy-button"
57-
>
58-
<span local-class="selectable">{{this.cargoAddCommand}}</span>
59-
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
60-
</CopyButton>
61-
{{else}}
62-
<code local-class="copy-fallback">
63-
{{this.cargoAddCommand}}
64-
</code>
65-
{{/if}}
66-
67-
<p local-class="copy-help">Or add the following line to your Cargo.toml:</p>
68-
{{#if (is-clipboard-supported)}}
69-
<CopyButton
70-
@copyText={{this.tomlSnippet}}
71-
title="Copy Cargo.toml snippet to clipboard"
72-
local-class="copy-button"
73-
>
74-
<span local-class="selectable">{{this.tomlSnippet}}</span>
75-
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
76-
</CopyButton>
77-
{{else}}
78-
<code local-class="copy-fallback">
79-
{{this.tomlSnippet}}
80-
</code>
81-
{{/if}}
51+
<CrateSidebar::InstallInstructions
52+
@crate={{@crate.name}}
53+
@version={{@version.num}}
54+
@exactVersion={{@requestedVersion}}
55+
@hasLib={{not (@version.has_lib false)}}
56+
@binNames={{@version.bin_names}}
57+
/>
8258
</div>
8359
{{/unless}}
8460

app/components/crate-sidebar.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ export default class CrateSidebar extends Component {
1414
return homepage && (!repository || simplifyUrl(repository) !== simplifyUrl(homepage));
1515
}
1616

17-
get cargoAddCommand() {
18-
return this.args.requestedVersion
19-
? `cargo add ${this.args.crate.name}@=${this.args.requestedVersion}`
20-
: `cargo add ${this.args.crate.name}`;
21-
}
22-
23-
get tomlSnippet() {
24-
let version = this.args.version.num.split('+')[0];
25-
let exact = this.args.requestedVersion ? '=' : '';
26-
return `${this.args.crate.name} = "${exact}${version}"`;
27-
}
28-
2917
get playgroundLink() {
3018
let playgroundCrates = this.playground.crates;
3119
if (!playgroundCrates) return;

app/components/crate-sidebar.module.css

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -50,60 +50,6 @@
5050
font-variant-numeric: tabular-nums;
5151
}
5252

53-
.copy-help {
54-
font-size: 12px;
55-
}
56-
57-
.copy-button,
58-
.copy-fallback {
59-
display: flex;
60-
width: 100%;
61-
align-items: center;
62-
justify-content: space-between;
63-
padding: var(--space-2xs) var(--space-xs);
64-
font-family: var(--font-monospace);
65-
font-size: 14px;
66-
line-height: 1.5em;
67-
color: var(--main-color);
68-
background: transparent;
69-
border-radius: var(--space-3xs);
70-
border: solid var(--space-4xs) var(--gray-border);
71-
72-
span {
73-
flex: auto;
74-
display: block;
75-
word-break: break-word;
76-
}
77-
}
78-
79-
.copy-button {
80-
text-align: start;
81-
cursor: pointer;
82-
83-
&:hover {
84-
background-color: light-dark(white, #141413);
85-
}
86-
}
87-
88-
.copy-icon {
89-
flex-shrink: 0;
90-
height: 1.1em;
91-
width: auto;
92-
/* for slightly nicer alignment... */
93-
margin-top: -3px;
94-
margin-left: var(--space-2xs);
95-
opacity: 0;
96-
transition: opacity var(--transition-fast);
97-
98-
.copy-button:hover & {
99-
opacity: 1;
100-
}
101-
}
102-
103-
.selectable {
104-
user-select: text;
105-
}
106-
10753
.links {
10854
> * + * {
10955
margin-top: var(--space-m);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{{#if @binNames}}
2+
{{#if (is-clipboard-supported)}}
3+
<CopyButton
4+
@copyText={{this.cargoInstallCommand}}
5+
title="Copy command to clipboard"
6+
local-class="copy-button"
7+
>
8+
<span local-class="selectable">{{this.cargoInstallCommand}}</span>
9+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
10+
</CopyButton>
11+
{{else}}
12+
<code local-class="copy-fallback">
13+
{{this.cargoInstallCommand}}
14+
</code>
15+
{{/if}}
16+
17+
<p local-class="copy-help">
18+
{{#if (eq @binNames.length 1)}}
19+
Running the above command will globally install the
20+
<span local-class="bin-name">{{get @binNames 0}}</span>
21+
binary.
22+
{{else if (eq @binNames.length 2)}}
23+
Running the above command will globally install the
24+
<span local-class="bin-name">{{get @binNames 0}}</span>
25+
and
26+
<span local-class="bin-name">{{get @binNames 1}}</span>
27+
binaries.
28+
{{else}}
29+
Running the above command will globally install these binaries:
30+
{{#each @binNames as |binName index|~}}
31+
{{~#if (eq index 0)~}}
32+
<span local-class="bin-name">{{binName}}</span>
33+
{{~else if (eq index (sum @binNames.length -1))}}
34+
and <span local-class="bin-name">{{binName}}</span>
35+
{{~else~}}
36+
, <span local-class="bin-name">{{binName}}</span>
37+
{{~/if}}
38+
{{~/each}}
39+
{{/if}}
40+
</p>
41+
42+
{{/if}}
43+
44+
{{#if (and @hasLib @binNames)}}
45+
<h3>Install as library</h3>
46+
{{/if}}
47+
48+
{{#if @hasLib}}
49+
<p local-class="copy-help">Run the following Cargo command in your project directory:</p>
50+
51+
{{#if (is-clipboard-supported)}}
52+
<CopyButton
53+
@copyText={{this.cargoAddCommand}}
54+
title="Copy command to clipboard"
55+
local-class="copy-button"
56+
>
57+
<span local-class="selectable">{{this.cargoAddCommand}}</span>
58+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
59+
</CopyButton>
60+
{{else}}
61+
<code local-class="copy-fallback">
62+
{{this.cargoAddCommand}}
63+
</code>
64+
{{/if}}
65+
66+
<p local-class="copy-help">Or add the following line to your Cargo.toml:</p>
67+
68+
{{#if (is-clipboard-supported)}}
69+
<CopyButton
70+
@copyText={{this.tomlSnippet}}
71+
title="Copy Cargo.toml snippet to clipboard"
72+
local-class="copy-button"
73+
>
74+
<span local-class="selectable">{{this.tomlSnippet}}</span>
75+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
76+
</CopyButton>
77+
{{else}}
78+
<code local-class="copy-fallback">
79+
{{this.tomlSnippet}}
80+
</code>
81+
{{/if}}
82+
{{/if}}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Component from '@glimmer/component';
2+
3+
export default class InstallInstructions extends Component {
4+
get cargoInstallCommand() {
5+
return this.args.exactVersion
6+
? `cargo install ${this.args.crate}@${this.args.version}`
7+
: `cargo install ${this.args.crate}`;
8+
}
9+
10+
get cargoAddCommand() {
11+
return this.args.exactVersion
12+
? `cargo add ${this.args.crate}@=${this.args.version}`
13+
: `cargo add ${this.args.crate}`;
14+
}
15+
16+
get tomlSnippet() {
17+
let version = this.args.version.split('+')[0];
18+
let exact = this.args.exactVersion ? '=' : '';
19+
return `${this.args.crate} = "${exact}${version}"`;
20+
}
21+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
.copy-help {
2+
font-size: 12px;
3+
overflow-wrap: break-word;
4+
}
5+
6+
.copy-button,
7+
.copy-fallback {
8+
display: flex;
9+
width: 100%;
10+
align-items: center;
11+
justify-content: space-between;
12+
padding: var(--space-2xs) var(--space-xs);
13+
font-family: var(--font-monospace);
14+
font-size: 14px;
15+
line-height: 1.5em;
16+
color: var(--main-color);
17+
background: transparent;
18+
border-radius: var(--space-3xs);
19+
border: solid var(--space-4xs) var(--gray-border);
20+
21+
span {
22+
flex: auto;
23+
display: block;
24+
word-break: break-word;
25+
}
26+
}
27+
28+
.copy-button {
29+
text-align: start;
30+
cursor: pointer;
31+
32+
&:hover {
33+
background-color: light-dark(white, #141413);
34+
}
35+
}
36+
37+
.copy-icon {
38+
flex-shrink: 0;
39+
height: 1.1em;
40+
width: auto;
41+
/* for slightly nicer alignment... */
42+
margin-top: -3px;
43+
margin-left: var(--space-2xs);
44+
opacity: 0;
45+
transition: opacity var(--transition-fast);
46+
47+
.copy-button:hover & {
48+
opacity: 1;
49+
}
50+
}
51+
52+
.selectable {
53+
user-select: text;
54+
}
55+
56+
.bin-name {
57+
font-family: var(--font-monospace);
58+
font-weight: bold;
59+
}

app/helpers/sum.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { helper } from '@ember/component/helper';
2+
3+
export default helper(function ([...values]) {
4+
return values.reduce((a, b) => a + b, 0);
5+
});

app/models/version.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default class Version extends Model {
2828
*/
2929
@attr rust_version;
3030

31+
/** @type {boolean | null} */
32+
@attr has_lib;
33+
/** @type {string[] | null} */
34+
@attr bin_names;
35+
3136
@belongsTo('crate', { async: false, inverse: 'versions' }) crate;
3237

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

0 commit comments

Comments
 (0)