Skip to content

Commit

Permalink
Merge pull request #435 from Zemnmez/csvpretty
Browse files Browse the repository at this point in the history
csvpretty
  • Loading branch information
Zemnmez authored Jun 23, 2022
2 parents adca791 + fdd0c93 commit 2f74d9f
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 41 deletions.
14 changes: 6 additions & 8 deletions css/lint/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

load("@build_bazel_rules_nodejs//:index.bzl", "js_library")

package(default_visibility = ["//:__subpackages__"])
Expand All @@ -19,14 +18,13 @@ exports_files([
"stylelint-config.json",
])


js_library(
name = "stylelint_config",
srcs = ["stylelint-config.json"],
deps = [
"@npm//stylelint/bin:stylelint",
"@npm//stylelint-config-standard",
"@npm//stylelint-config-recommended",
"@npm//stylelint-config-css-modules",
"@npm//stylelint-config-css-modules",
"@npm//stylelint-config-recommended",
"@npm//stylelint-config-standard",
"@npm//stylelint/bin:stylelint",
],
srcs = [ "stylelint-config.json" ],
)
)
10 changes: 6 additions & 4 deletions css/lint/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ load("@npm//stylelint:index.bzl", "stylelint_test")
def css_lint(name = None, srcs = [], **kwargs):
stylelint_test(
name = name,
data = [ "//css/lint:stylelint_config" ] + srcs,
data = ["//css/lint:stylelint_config"] + srcs,
templated_args = [
"--config", "css/lint/stylelint-config.json"
"--config",
"css/lint/stylelint-config.json",
] + [
"$(rootpaths " + x + ")" for x in srcs
"$(rootpaths " + x + ")"
for x in srcs
],
**kwargs
)
)
8 changes: 3 additions & 5 deletions css/providers.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


CSSLibraryInfo = provider(
doc = "Contains information about a set of CSS import files.",
fields = {
"srcs": "A depset of CSS files for this immediate group of CSS files.",
"deps": "A depset of CSSLibraryInfo structs for this library's dependencies."
}
"deps": "A depset of CSSLibraryInfo structs for this library's dependencies.",
},
)

def css_library_info(srcs, deps = []):
Expand All @@ -17,4 +15,4 @@ def css_library_info(srcs, deps = []):
return CSSLibraryInfo(
srcs = srcs,
deps = depset(transitive = transitive_depsets),
)
)
29 changes: 15 additions & 14 deletions css/rules.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
load("//:rules.bzl", "ts_project")
load("//css:providers.bzl", "css_library_info", "CSSLibraryInfo")
load("//css:providers.bzl", "CSSLibraryInfo", "css_library_info")
load("//css/lint:rules.bzl", "css_lint")


def css_module(name = None, srcs = [], **kwargs):
tsfilerulename = name + "_gen_ts"
outputtsfiles = [x + ".ts" for x in srcs]
Expand All @@ -23,35 +22,37 @@ def css_module(name = None, srcs = [], **kwargs):
ignores_lint = srcs,
)



def _css_library_impl(ctx):
css_info = css_library_info(
srcs = depset(ctx.files.srcs),
deps = [ x[CSSLibraryInfo] for x in ctx.attr.deps if CSSLibraryInfo in x]
srcs = depset(ctx.files.srcs),
deps = [x[CSSLibraryInfo] for x in ctx.attr.deps if CSSLibraryInfo in x],
)

return [
css_info,

DefaultInfo(
files = css_info.deps
)
files = css_info.deps,
),
]

_css_library_rule = rule(
implementation = _css_library_impl,
attrs = {
"srcs": attr.label_list(allow_files = True, doc = "A set of CSS files to group in a library."),
"deps": attr.label_list(allow_files = False, providers = [ CSSLibraryInfo ], doc = "A set of CSS libraries this group depends on.")
}
"deps": attr.label_list(allow_files = False, providers = [CSSLibraryInfo], doc = "A set of CSS libraries this group depends on."),
},
)

def css_library(name = None, srcs = None, deps = None, **kwargs):
_css_library_rule(
name = name,
srcs = srcs, deps = deps, **kwargs
srcs = srcs,
deps = deps,
**kwargs
)
css_lint(
name = name + "_lint",
srcs = srcs, deps = deps, **kwargs)
srcs = srcs,
deps = deps,
**kwargs
)
25 changes: 25 additions & 0 deletions go/cmd/csvpretty/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("//:rules.bzl", "go_binary")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")

go_binary(
name = "csvpretty",
srcs = glob(["*.go"]),
visibility = ["//visibility:public"],
)

genrule(
name = "csvpretty_integration_gen",
tools = [ ":csvpretty" ],
srcs = [ ":test.csv" ],
outs = [ "test_output.csv" ],
cmd = """
$(execpath :csvpretty) --input $(rootpath :test.csv) --output $@
"""
)


diff_test(
name = "csvpretty_integration",
file1 = "test_output.csv",
file2 = "test_out.csv"
)
21 changes: 21 additions & 0 deletions go/cmd/csvpretty/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Zemnmez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
118 changes: 118 additions & 0 deletions go/cmd/csvpretty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
CSVPretty
=============================================================================
[CSVPretty]: #CSVPretty

CSVPretty is a small tool that pretty prints CSV files.

There are many tools online that claim to pretty print CSV files when
really they are just reformatted for display. This tool produces files
which actually *are* CSV files, just with the columns aligned with the
[Elastic Tabstops] algorithm.

The CSV files should produce identical data when parsed provided your
CSV parser is configured to drop preceeding whitespace in CSV records
(see [Limitations]).

[Elastic Tabstops]: https://godoc.org/text/tabwriter



Example
-----------------------------------------------------------------------------
[Example]: #example

For the input:

[test.csv ](test.csv)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ csv
Name, Subspecies, Species, Genus
West African Giraffe, G. c. peralta, G. camelopardalis, Giraffa
Meerkat, , s.Suricata, Suricata
Dog, C. l. familiaris, C. lupus, Canis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



`csvpretty -input test.csv -output test_out.csv` produces:

[test_out.csv ](test_out.csv)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ csv
Name, Subspecies, Species
West African Giraffe, G. c. peralta, G. camelopardalis
Meerkat, , s.Suricata
Dog, C. l. familiaris, C. lupus
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Installation
-----------------------------------------------------------------------------
[Installation]: #Installation

With a working Go setup, and `$GOPATH/bin` in your `$PATH`:
```bash
go get github.com/zemnmez/csvpretty -u
```

If you want to quickly use `csvpretty` and you have `go` installed, you can
just:

```bash
go run github.com/zemnmez/csvpretty
```

With whatever args you desire.


Usage With `go generate`
-----------------------------------------------------------------------------
You can use `csvpretty` in conjunction with `go generate` to automatically
pretty print CSVs. This is best used in conjection with `go mod`, which will
manage your tooling versions automagically.

To do this, add a `go generate` line to a `.go` file like this:

```go
//go:generate go run github.com/zemnmez/csvpretty -input myfile.csv -w
```


Limitations
-----------------------------------------------------------------------------
[Limitations]: #limitations

The only limitation to CSVPretty is that your CSV parser drops leading
whitespace in records. This is a common option that's tenuously
considered part of the CSV format. In Go, for example you can set the
TrimLeadingSpace field of [csv.Reader] to `true` for this.

[csv.Reader]: https://godoc.org/encoding/csv



License
-----------------------------------------------------------------------------
[License]: #license


MIT License

Copyright (c) 2019 Zemnmez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 2f74d9f

Please sign in to comment.