Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨feat: .selene.toml and selene.toml are now detected by order of priority #591

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6ad27f6
✨feat: `selene.toml` and `.selene.toml` are not detected by order of …
Zeioth Mar 10, 2024
02213f4
🐛fix(`.selene`): has now order of precedence over `selene.toml`.
Zeioth Mar 13, 2024
b4a93e3
🐛fix(error!): instead of eprintln.
Zeioth Mar 13, 2024
13e6286
♻️refactor(read_config_file): Is now a function.
Zeioth Mar 13, 2024
9e91ded
Update selene/src/main.rs
Zeioth Mar 15, 2024
de9aa79
fix: Terminate the process when config file is invalid.
Zeioth Mar 17, 2024
8947dea
fix: We now check that the file exist. Also we only raise a error if …
Zeioth Mar 18, 2024
406558f
fix(regression): Ensure it fallback on the default config.
Zeioth Mar 18, 2024
f7785fb
refactor of the former
Zeioth Mar 18, 2024
5def22b
♻️refactor: All should be fine now, but we can still refactor the fun…
Zeioth Mar 18, 2024
69d6d98
♻️refactor: All config file detection logic has been moved to functions.
Zeioth Mar 18, 2024
64bbb82
comments: More consistent.
Zeioth Mar 18, 2024
d0cb38e
Update CHANGELOG.md
Zeioth Mar 18, 2024
e5fd4f0
docs: updated for `.selene.toml`.
Zeioth Mar 18, 2024
d7de32e
Merge remote-tracking branch 'refs/remotes/origin/dot-selene-toml'
Zeioth Mar 18, 2024
1c425b8
fix(comments): Our comments were preventing `cargo rustdoc` from runn…
Zeioth Mar 18, 2024
0ef6e80
Update selene/src/main.rs
Zeioth Mar 26, 2024
ef996c4
Update selene/src/main.rs
Zeioth Mar 26, 2024
822bc44
refactor: config_paths_to_check is now a constant.
Zeioth Mar 26, 2024
ade463c
refactor: read_config_file() -> (String, Option<PathBuf>) the type re…
Zeioth Mar 27, 2024
3812d6b
refactor: Now that we handle the bad utf-8 error correctly, let's add…
Zeioth Mar 27, 2024
47b0642
vscode: relevant changes to vscode extension.
Zeioth Mar 27, 2024
4ca19d4
♻️refactor: We now let rust manage the invalid UTF-8 error instead of…
Zeioth May 7, 2024
aff6f9a
🐛fix(config file detection): If path exists and fs::read_to_string er…
Zeioth May 8, 2024
7c844a3
🐛refactor: There is no need for us to explicitely read the file as ut…
Zeioth May 8, 2024
5c02fe9
comments: Better info, in case we decide to enable this.
Zeioth May 8, 2024
9829822
fix: The error now works as originally intended → `ERROR: Error readi…
Zeioth May 21, 2024
ed5e764
Update main.rs → comments
Zeioth May 21, 2024
7fa7825
fix: Display the next error ONLY on validate-config → `ERROR: Error r…
Zeioth May 21, 2024
8817d13
Solve merge conflict
Zeioth May 21, 2024
04ed488
Merge branch 'main' into dot-selene-toml
chriscerie Jun 29, 2024
dcf3458
Refactor
chriscerie Jun 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased](https://github.com/Kampfkarren/selene/compare/0.27.1...HEAD)
### Added
- Added `Path2DControlPoint.new` to the Roblox standard library
- Added `.selene.toml` as possible config file with priority over `selene.toml`.

## [0.27.1](https://github.com/Kampfkarren/selene/releases/tag/0.27.1) - 2024-04-28
### Fixed
Expand Down Expand Up @@ -437,4 +438,4 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added standard library chaining. This means you can combine two standard libraries by setting `std` in selene.toml to `std1+std2`. You can chain as many as you want.

## [0.1.0](https://github.com/Kampfkarren/selene/releases/tag/0.1.0) - 2019-11-06
- Initial release
- Initial release
2 changes: 1 addition & 1 deletion docs/src/cli/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FLAGS:

OPTIONS:
--color <color> [default: auto] [possible values: Always, Auto, Never]
--config <config> A toml file to configure the behavior of selene [default: selene.toml]
--config <config> A toml file to configure the behavior of selene [default: .selene.toml]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't technically accurate because the default is both selene.toml and .selene.toml.

Copy link
Author

@Zeioth Zeioth Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is .selene.toml because it has precedence over selene.toml,
The only place where selene.toml is mentioned as a possible alternative is docs/src/usagi/configuration to avoid duplicity that might make the docs harder to understand.

If you want to have a different string there, please tell me exactly, or edit the PR.

--display-style <display-style> Sets the display method [possible values: Json, Json2, Rich, Quiet]
--num-threads <num-threads> Number of threads to run on, default to the numbers of logical cores on your
system [default: your system's cores]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lints/high_cyclomatic_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end

## Remarks

This lint is off by default. In order to enable it, add this to your selene.toml:
This lint is off by default. In order to enable it, add this to your .selene.toml:

```toml
[lints]
Expand Down
6 changes: 3 additions & 3 deletions docs/src/roblox.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ If you try to run selene on a Roblox codebase, you'll get a bunch of errors sayi

## Installation

Thankfully, this process is very simple. All you need to do is edit your `selene.toml` (or create one) and add the following:
Thankfully, this process is very simple. All you need to do is edit your `.selene.toml` (or create one) and add the following:

```toml
std = "roblox"
```

The next time you run selene, or if you use the Visual Studio Code extension and start typing Lua code, a Roblox standard library will be automatically generated and used. This is an automatic process that occurs whenever you don't have a cached standard library file and your `selene.toml` has `std = "roblox"`.
The next time you run selene, or if you use the Visual Studio Code extension and start typing Lua code, a Roblox standard library will be automatically generated and used. This is an automatic process that occurs whenever you don't have a cached standard library file and your `.selene.toml` has `std = "roblox"`.

## Updating definitions

Expand All @@ -32,7 +32,7 @@ There may be cases where you would rather not have selene automatically update t

selene supports "pinning" the standard library to a specific version.

Add the following to your `selene.toml` configuration:
Add the following to your `.selene.toml` configuration:
```toml
# `floating` by default, meaning it is stored in a cache folder on your system
roblox-std-source = "pinned"
Expand Down
10 changes: 5 additions & 5 deletions docs/src/usage/configuration.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Configuration
selene is meant to be easily configurable. You can specify configurations for the entire project as well as for individual lints.

Configuration files are placed in the directory you are running selene in and are named **selene.toml**. As the name suggests, the configurations use the [Tom's Obvious, Minimal Language (TOML)](https://github.com/toml-lang/toml) format. It is recommended you quickly brush up on the syntax, though it is very easy.
Configuration files are placed in the directory you are running selene in and are named **.selene.toml** (or **selene.toml**) . As the name suggests, the configurations use the [Tom's Obvious, Minimal Language (TOML)](https://github.com/toml-lang/toml) format. It is recommended you quickly brush up on the syntax, though it is very easy.

## Changing the severity of lints
You can change the severity of lints by entering the following into selene.toml:
You can change the severity of lints by entering the following into .selene.toml:

```toml
[lints]
Expand All @@ -22,7 +22,7 @@ Where "severity" is one of the following:
Note that "deny" and "warn" are effectively the same, only warn will give orange text while error gives red text, and they both have different counters.

## Configuring specific lints
You can configure specific lints by entering the following into selene.toml:
You can configure specific lints by entering the following into .selene.toml:

```toml
[config]
Expand All @@ -47,7 +47,7 @@ By default, selene uses Lua 5.1, though if we wanted to use the Lua 5.2 standard
std = "lua52"
```

...at the top of selene.toml. You can learn more about the standard library format on the [standard library guide](./std.md). The standard library given can either be one of the builtin ones (currently only `lua51` and `lua52`) or the filename of a standard library file in this format. For example, if we had a file named `special.toml`, we would write:
...at the top of .selene.toml. You can learn more about the standard library format on the [standard library guide](./std.md). The standard library given can either be one of the builtin ones (currently only `lua51` and `lua52`) or the filename of a standard library file in this format. For example, if we had a file named `special.toml`, we would write:

```toml
std = "special"
Expand All @@ -68,4 +68,4 @@ It is possible to exclude files from being linted using the exclude option:

```toml
exclude = ["external/*", "*.spec.lua"]
```
```
2 changes: 1 addition & 1 deletion docs/src/usage/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ However, perhaps we as the programmer have some reason for leaving this unused (
local something = 1
```

This also works with settings other than `allow`--you can `warn` or `deny` lints in the same fashion. For example, you can have a project with the following `selene.toml` [configuration](./configuration.md):
This also works with settings other than `allow`--you can `warn` or `deny` lints in the same fashion. For example, you can have a project with the following `.selene.toml` [configuration](./configuration.md):

```toml
[lints]
Expand Down
3 changes: 2 additions & 1 deletion selene-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"activationEvents": [
"onLanguage:lua",
"onLanguage:luau",
"workspaceContains:.selene.toml",
"workspaceContains:selene.toml",
"onCommand:selene.reinstall",
"onCommand:selene.update-roblox-std",
Expand Down Expand Up @@ -118,4 +119,4 @@
"semver": "^7.3.8",
"unzipper": "^0.10.11"
}
}
}
3 changes: 2 additions & 1 deletion selene-vscode/src/configLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function lintConfig(

if (
document.languageId === "toml" &&
!document.uri.path.endsWith(".selene.toml") &&
!document.uri.path.endsWith("selene.toml")
) {
return
Expand Down Expand Up @@ -81,4 +82,4 @@ export async function lintConfig(
}

diagnosticsCollection.set(document.uri, diagnostics)
}
}
58 changes: 33 additions & 25 deletions selene-vscode/src/roblox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,52 @@ export function processDiagnostic(
return
}

const configFilename = vscode.Uri.joinPath(
workspace.uri,
"selene.toml",
)
const configFilenames = [
vscode.Uri.joinPath(workspace.uri, ".selene.toml"),
vscode.Uri.joinPath(workspace.uri, "selene.toml"),
]

let configContents: Uint8Array

try {
configContents = await vscode.workspace.fs.readFile(
configFilename,
)
} catch (error) {
if (
error instanceof vscode.FileSystemError &&
error.code === "FileNotFound"
) {
configContents = new Uint8Array()
} else {
vscode.window.showErrorMessage(
`Couldn't read existing config, if there was one.\n\n${
typeof error === "object" && error !== null
let configFilename: vscode.Uri | undefined

for (const someConfigFilename of configFilenames) {
try {
configContents = await vscode.workspace.fs.readFile(someConfigFilename)
configFilename = someConfigFilename
break
} catch (error) {
if (
error instanceof vscode.FileSystemError &&
error.code === "FileNotFound"
) {
continue
} else {
vscode.window.showErrorMessage(
`Couldn't read existing config, if there was one.\n\n${typeof error === "object" && error !== null
? error.toString()
: error
}`,
)

return
}`
)
return
}
}
}

if (!configFilename) {
configFilename = configFilenames[0]
configContents = new Uint8Array()
}

const contents = new TextDecoder().decode(configContents)

vscode.workspace.fs.writeFile(
configFilename,
new TextEncoder().encode(addRobloxLibrary(contents)),
new TextEncoder().encode(addRobloxLibrary(contents))
)
})



return true
}

Expand Down Expand Up @@ -103,4 +111,4 @@ function addRobloxLibrary(contents: string): string {
return standardLibrarySet
? lines.join("\n")
: `std = "roblox"\n${lines.join("\n")}`
}
}
55 changes: 31 additions & 24 deletions selene/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use codespan_reporting::{
diagnostic::{
Diagnostic as CodespanDiagnostic, Label as CodespanLabel, Severity as CodespanSeverity,
},
term::DisplayStyle as CodespanDisplayStyle,
};
use selene_lib::{lints::Severity, *};
use std::{
ffi::OsString,
fmt, fs,
Expand All @@ -8,14 +15,6 @@ use std::{
Arc, RwLock,
},
};

use codespan_reporting::{
diagnostic::{
Diagnostic as CodespanDiagnostic, Label as CodespanLabel, Severity as CodespanSeverity,
},
term::DisplayStyle as CodespanDisplayStyle,
};
use selene_lib::{lints::Severity, *};
use structopt::{clap, StructOpt};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use threadpool::ThreadPool;
Expand Down Expand Up @@ -391,6 +390,22 @@ fn read_file(checker: &Checker<toml::value::Value>, filename: &Path) {
);
}

fn read_config() -> Result<(String, PathBuf), String> {
const CONFIG_PATHS_TO_CHECK: [&str; 2] = [".selene.toml", "selene.toml"];

CONFIG_PATHS_TO_CHECK
.iter()
.find_map(|path_str| {
let path = Path::new(path_str);

path.exists().then_some(match fs::read_to_string(path_str) {
Ok(contents) => Ok((contents, path.to_path_buf())),
Err(error) => Err(format!("Error reading config file: {error}")),
})
})
.unwrap_or(Err("No config file found".to_string()))
}

fn start(mut options: opts::Options) {
*OPTIONS.write().unwrap() = Some(options.clone());

Expand All @@ -410,23 +425,16 @@ fn start(mut options: opts::Options) {
std::process::exit(1);
}

(config_contents, Path::new("-"))
(config_contents, Path::new("-").to_path_buf())
} else {
let config_path = Path::new("selene.toml");

let config_contents = match fs::read_to_string(config_path) {
Ok(contents) => contents,
Err(error) => {
error!("Error reading config file: {error}");
std::process::exit(1);
}
};

(config_contents, config_path)
read_config().unwrap_or_else(|error| {
error!("{error}");
std::process::exit(1);
})
};

if let Err(error) = validate_config::validate_config(
config_path,
&config_path,
&config_contents,
&std::env::current_dir().unwrap(),
) {
Expand Down Expand Up @@ -517,15 +525,14 @@ fn start(mut options: opts::Options) {
}
}

None => match fs::read_to_string("selene.toml") {
Ok(config_contents) => match toml::from_str(&config_contents) {
None => match read_config() {
Ok((config_contents, _)) => match toml::from_str(&config_contents) {
Ok(config) => (config, None),
Err(error) => {
error!("Config file not in correct format: {}", error);
std::process::exit(1);
}
},

Err(_) => (CheckerConfig::default(), None),
},
};
Expand Down
Loading