Skip to content

Commit c82a6d1

Browse files
authored
Soft deprecate --json instead of --format json in workspace.list (#12)
1 parent 9329713 commit c82a6d1

File tree

2 files changed

+221
-21
lines changed

2 files changed

+221
-21
lines changed

workspace/lib/mix/tasks/workspace.list.ex

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
defmodule Mix.Tasks.Workspace.List do
22
opts = [
3+
format: [
4+
type: :string,
5+
default: "pretty",
6+
doc: """
7+
The output format of the list. It can be one of the following:
8+
* `json` - pretty prints the list as a json.
9+
* `pretty` - pretty prints the list.
10+
""",
11+
allowed: ["json", "pretty"]
12+
],
313
json: [
414
type: :boolean,
5-
default: false,
615
doc: """
716
If set a `json` file will be generated with the list of workspace projects and
817
associated metadata. By default it will be saved in `workspace.json` in the
918
current directory. You can override the output path by setting the `--output`
1019
option.
1120
""",
21+
deprecated: "Use `--format json` with `--output` instead.",
1222
doc_section: :export
1323
],
1424
output: [
1525
type: :string,
16-
default: "workspace.json",
1726
doc: """
18-
The output file. Applicable only if `--json` is set.
27+
Save the list to a file. Applicable only if `--format` is set to `json`.
1928
""",
2029
doc_section: :export
2130
],
@@ -24,7 +33,7 @@ defmodule Mix.Tasks.Workspace.List do
2433
default: false,
2534
doc: """
2635
If set the paths in the exported json file will be relative with respect to the
27-
workspace path. Applicable only if `--json` is set.
36+
workspace path. Applicable only if `--format` is set to `json`.
2837
""",
2938
doc_section: :export
3039
],
@@ -162,13 +171,18 @@ defmodule Mix.Tasks.Workspace.List do
162171
end
163172

164173
defp list_or_save_workspace_projects(workspace, opts) do
165-
case opts[:json] do
166-
false -> list_workspace_projects(workspace, opts[:show_status])
167-
true -> write_json(workspace, opts)
168-
end
174+
# TODO: remove --json support in 0.4.0
175+
opts =
176+
if opts[:json] do
177+
Keyword.merge(opts, format: "json", output: opts[:output] || "workspace.json")
178+
else
179+
opts
180+
end
181+
182+
output_result(workspace, opts, opts[:format])
169183
end
170184

171-
defp list_workspace_projects(workspace, show_status) do
185+
defp output_result(workspace, opts, "pretty") do
172186
projects = Workspace.projects(workspace)
173187

174188
case Enum.count(projects, &(not &1.skip)) do
@@ -184,13 +198,26 @@ defmodule Mix.Tasks.Workspace.List do
184198
:reset,
185199
" matching the given options."
186200
])
201+
202+
max_project_length = max_project_length(projects)
203+
204+
projects
205+
|> Enum.sort_by(& &1.app)
206+
|> Enum.each(&print_project_info(&1, max_project_length, opts[:show_status]))
187207
end
208+
end
188209

189-
max_project_length = max_project_length(projects)
210+
defp output_result(workspace, opts, "json") do
211+
json_data = Workspace.Export.to_json(workspace, sort: true, relative: opts[:relative_paths])
190212

191-
projects
192-
|> Enum.sort_by(& &1.app)
193-
|> Enum.each(&print_project_info(&1, max_project_length, show_status))
213+
case opts[:output] do
214+
nil ->
215+
IO.puts(json_data)
216+
217+
output ->
218+
File.write!(output, json_data)
219+
Workspace.Cli.log([:green, "generated ", :reset, output], prefix: :header)
220+
end
194221
end
195222

196223
defp max_project_length([]), do: 0
@@ -223,14 +250,6 @@ defmodule Mix.Tasks.Workspace.List do
223250
defp description(nil), do: ""
224251
defp description(doc) when is_binary(doc), do: [" - ", doc]
225252

226-
defp write_json(workspace, opts) do
227-
json_data = Workspace.Export.to_json(workspace, sort: true, relative: opts[:relative_paths])
228-
229-
File.write!(opts[:output], json_data)
230-
231-
Workspace.Cli.log([:green, "generated ", :reset, opts[:output]], prefix: :header)
232-
end
233-
234253
defp tags([]), do: []
235254

236255
defp tags(tags) do

workspace/test/mix/tasks/workspace.list_test.exs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,185 @@ defmodule Mix.Tasks.Workspace.ListTest do
418418
git: true
419419
)
420420
end
421+
422+
@tag :tmp_dir
423+
test "with --format pretty option set", %{tmp_dir: tmp_dir} do
424+
Workspace.Test.with_workspace(
425+
tmp_dir,
426+
[],
427+
:default,
428+
fn ->
429+
expected = """
430+
Found 11 workspace projects matching the given options.
431+
* :package_a package_a/mix.exs :shared, area:core
432+
* :package_b package_b/mix.exs - a dummy project
433+
* :package_c package_c/mix.exs
434+
* :package_d package_d/mix.exs
435+
* :package_e package_e/mix.exs
436+
* :package_f package_f/mix.exs
437+
* :package_g package_g/mix.exs
438+
* :package_h package_h/mix.exs
439+
* :package_i package_i/mix.exs
440+
* :package_j package_j/mix.exs
441+
* :package_k nested/package_k/mix.exs
442+
"""
443+
444+
assert capture_io(fn ->
445+
ListTask.run([
446+
"--workspace-path",
447+
tmp_dir,
448+
"--format",
449+
"pretty"
450+
])
451+
end) == expected
452+
end
453+
)
454+
end
455+
456+
@tag :tmp_dir
457+
test "with --format json option set", %{tmp_dir: tmp_dir} do
458+
Workspace.Test.with_workspace(
459+
tmp_dir,
460+
[],
461+
:default,
462+
fn ->
463+
output =
464+
capture_io(fn ->
465+
ListTask.run([
466+
"--workspace-path",
467+
tmp_dir,
468+
"--format",
469+
"json"
470+
])
471+
end)
472+
473+
data = Jason.decode!(output)
474+
475+
assert length(data["projects"]) == 11
476+
assert Path.type(data["workspace_path"]) == :absolute
477+
478+
for project <- data["projects"] do
479+
assert Path.type(project["workspace_path"]) == :absolute
480+
assert Path.type(project["mix_path"]) == :absolute
481+
assert Path.type(project["path"]) == :absolute
482+
end
483+
end
484+
)
485+
end
486+
487+
@tag :tmp_dir
488+
test "with --format json and --output option set", %{tmp_dir: tmp_dir} do
489+
Workspace.Test.with_workspace(
490+
tmp_dir,
491+
[],
492+
:default,
493+
fn ->
494+
Workspace.Test.modify_project(tmp_dir, "package_d")
495+
Workspace.Test.modify_project(tmp_dir, "package_e")
496+
497+
output = Path.join(tmp_dir, "workspace.json")
498+
499+
expected = """
500+
==> generated #{output}
501+
"""
502+
503+
assert capture_io(fn ->
504+
ListTask.run([
505+
"--workspace-path",
506+
tmp_dir,
507+
"--format",
508+
"json",
509+
"--output",
510+
output
511+
])
512+
end) == expected
513+
514+
data = File.read!(output) |> Jason.decode!()
515+
516+
assert length(data["projects"]) == 11
517+
assert Path.type(data["workspace_path"]) == :absolute
518+
519+
for project <- data["projects"] do
520+
assert Path.type(project["workspace_path"]) == :absolute
521+
assert Path.type(project["mix_path"]) == :absolute
522+
assert Path.type(project["path"]) == :absolute
523+
end
524+
end,
525+
git: true
526+
)
527+
end
528+
529+
@tag :tmp_dir
530+
test "with --format json and --relative-paths", %{tmp_dir: tmp_dir} do
531+
Workspace.Test.with_workspace(
532+
tmp_dir,
533+
[],
534+
:default,
535+
fn ->
536+
output = Path.join(tmp_dir, "workspace.json")
537+
538+
expected = """
539+
==> generated #{output}
540+
"""
541+
542+
assert capture_io(fn ->
543+
ListTask.run([
544+
"--workspace-path",
545+
tmp_dir,
546+
"--format",
547+
"json",
548+
"--output",
549+
output,
550+
"--relative-paths"
551+
])
552+
end) == expected
553+
554+
data = File.read!(output) |> Jason.decode!()
555+
556+
assert data["workspace_path"] == "."
557+
assert length(data["projects"]) == 11
558+
559+
for project <- data["projects"] do
560+
assert project["workspace_path"] == "."
561+
assert Path.type(project["mix_path"]) == :relative
562+
assert Path.type(project["path"]) == :relative
563+
end
564+
end
565+
)
566+
end
567+
568+
@tag :tmp_dir
569+
test "with --format json and filtering options", %{tmp_dir: tmp_dir} do
570+
Workspace.Test.with_workspace(
571+
tmp_dir,
572+
[],
573+
:default,
574+
fn ->
575+
output = Path.join(tmp_dir, "workspace.json")
576+
577+
expected = """
578+
==> generated #{output}
579+
"""
580+
581+
assert capture_io(fn ->
582+
ListTask.run([
583+
"--workspace-path",
584+
tmp_dir,
585+
"--format",
586+
"json",
587+
"--output",
588+
output,
589+
"-e",
590+
"package_a",
591+
"-e",
592+
"package_b"
593+
])
594+
end) == expected
595+
596+
assert %{"projects" => projects} = File.read!(output) |> Jason.decode!()
597+
598+
assert length(projects) == 9
599+
end
600+
)
601+
end
421602
end

0 commit comments

Comments
 (0)