Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: "pub"
directory: "/"
schedule:
interval: "weekly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
verify:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Dart SDK
uses: dart-lang/setup-dart@v1

- name: Install dependencies
run: dart pub get

- name: Analyze
run: dart analyze .

- name: Check formatting
run: dart format -o none --set-exit-if-changed .

- name: Run tests
run: dart test
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This makefile leverages automatic documentation. Running `make` will generate a list
# of the most commonly used targets. `make help` will generate a more complete list.
#
# When adding a target, prefix the doc string with two pound signs to add it to the common
# list or two pounds and VERBOSE to demote it to the `make help` list. Prefix it with two pounds
# and INTERNAL to exclude from all help, or just don't add a doc string.
#
# Based on https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html

.DEFAULT_GOAL := help
SHELL=/bin/bash -o pipefail

# Supports:
# make install /usr/local/bin
# make install
INSTALLPATH ?= $(if $(word 2,$(MAKECMDGOALS)),$(word 2,$(MAKECMDGOALS)),$(HOME)/bin)

ifneq (,$(filter install,$(MAKECMDGOALS)))
ifneq ($(word 2,$(MAKECMDGOALS)),)
.PHONY: $(word 2,$(MAKECMDGOALS))
$(word 2,$(MAKECMDGOALS)):
@:
endif
endif

.PHONY: help
help: ##META Display all make targets
@printf "\33[32m"
@echo "All documented make targets. Use 'make' to see only the most commonly used targets."
@printf "\033[0m\n"

@grep -E '^%?[a-zA-Z0-9/_-]+:.*?##(VERBOSE)? .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?##(VERBOSE)? "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

# Compile both executables in bin
.PHONY: install
install: ## Build and install executables (default: ~/bin)
@mkdir -p "$(INSTALLPATH)"
dart compile exe bin/replace.dart -o "$(INSTALLPATH)/replace"
dart compile exe bin/pm.dart -o "$(INSTALLPATH)/pm"
176 changes: 168 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# replace

Easy to use cross-platform regex replace command line util.
Can't remember the arguments to the `find` command? or how `xargs` works?
Maybe `sed` is a little different on your Mac than in Linux?
Expand All @@ -10,27 +11,37 @@ It's meant to be used in a directory under source control so you can see
what files have been changed using a diff.

It ignores dotfiles (especially the .git directory) except these
- .gitignore
- .pubignore
- .travis.yml
- .travis.yaml

- .gitignore
- .pubignore
- .travis.yml
- .travis.yaml

Globs don't seem to go into dotfile directories by default so if you want to do that, just
include another glob in the command for those (Ex: `replace fish zebra .github/**.md **/*.md`)

If you need to replace in dotfiles other than these, you can submit a PR to this repo to
allow it or work around it by renaming the file, replacing, and renaming it back.

### Installing
## Installing

`pub global activate replace`

This installs both command line tools from this package:

- `replace` for regex find/replace in files
- `pm` for editing dependency constraints in pubspec.yaml files

or more advanced install

- clone the project
- `pub get`
- `dart compile exe bin/replace.dart -o replace`
- Place the `replace` executable in your path
- `dart compile exe bin/pm.dart -o pm`
- Place the `replace` and `pm` executable in your path

## How to use replace

### Running / Usage
`replace <regexp> <replacement> <glob_file_or_dir> ...`

This means you can pass as many globs, directory names, or filenames
Expand All @@ -45,7 +56,8 @@ replacement, you can use quotes and `noglob`.
Example: `noglob replace "key & peele" "ren || stimpy" **/*.md`

Regexes and globs are Dart style
Glob Syntax: https://pub.dev/packages/glob#syntax

Glob Syntax: [https://pub.dev/packages/glob#syntax](https://pub.dev/packages/glob#syntax)

The replacement may contain references to the capture groups in regexp using a backslash followed by the group number. Backslashes not followed by a number return the character immediately following them.

Expand All @@ -62,3 +74,151 @@ Match a word at the beginning of a line

Match a word at the end of a line
`replace "dessert$" cookies menu.txt`

## How to use pm

`pm` updates dependency version constraints in one or more pubspec.yaml files.

### Usage

`pm [global options] <command> <dependency> <version>`

Global options:

- `-h, --help` Print usage information
- `-r, --recursive` Recurse through subdirectories and process all pubspec.yaml files
- `--fail-on-parse-error` Exit with non-zero if any pubspec.yaml cannot be parsed
- `--[no-]tighten` Tighten is enabled by default. Use `--no-tighten` to keep explicit range output.

Commands:

- `set` Set dependency constraint exactly as provided
- `set-sdk` Set `environment.sdk` constraint exactly as provided
- `raise-min` Raise the minimum bound (inclusive) of a version range
- `raise-min-sdk` Raise the minimum `environment.sdk` bound (inclusive)
- `raise-max` Raise the maximum bound (exclusive) of a version range
- `raise-max-sdk` Raise the maximum `environment.sdk` bound (exclusive)
- `lower-max` Lower the maximum bound (exclusive) of a version range

Notes:

- Updates both `dependencies` and `dev_dependencies`
- SDK commands update `environment.sdk` only
- `set` accepts any valid Dart version constraint, for example `^1.9.0` or `'>=1.9.0 <2.0.0'`
- `set-sdk` accepts any valid Dart SDK constraint, for example `'>=3.3.0 <4.0.0'`
- `raise-min`, `raise-max`, and `lower-max` expect a specific semantic version, for example `1.9.1`
- `raise-min-sdk` and `raise-max-sdk` expect a specific semantic version, for example `3.4.0`
- Tightening is enabled by default and only rewrites when the updated range is exactly equivalent to a caret constraint
- `'>=3.0.0 <4.0.0'` becomes `^3.0.0`
- `'>=0.18.2 <0.19.0'` becomes `^0.18.2`
- `'>=1.2.3 <4.0.0'` stays as a range (not equivalent to a caret constraint)

### Examples

Set a dependency version:

```sh
pm set path 1.9.1
```

Set a single version to a range constraint:

```sh
pm set path '>=1.9.0 <2.0.0'
```

Before:

```yaml
dependencies:
path: 1.9.1
```

After:

```yaml
dependencies:
path: '>=1.9.0 <2.0.0'
```

Raise minimum version recursively across a monorepo:

```sh
pm raise-min path 1.9.1 -r
```

Raise minimum version recursively and opt out of tightening:

```sh
pm raise-min path 1.9.1 -r --no-tighten
```

Range to single version when the new minimum meets or exceeds the old maximum:

```sh
pm raise-min path 1.9.1
```

Before:

```yaml
dependencies:
path: '>=1.8.0 <1.9.1'
```

After:

```yaml
dependencies:
path: 1.9.1
```

Range to narrower range when the upper bound is still greater than the new minimum:

```sh
pm raise-min path 1.9.1
```

Before:

```yaml
dependencies:
path: '>=1.8.0 <2.0.0'
```

After:

```yaml
dependencies:
path: '>=1.9.1 <2.0.0'
```

Lower max version and fail if any pubspec is malformed:

```sh
pm lower-max path 2.5.0 -r --fail-on-parse-error
```

Set SDK constraint:

```sh
pm set-sdk '>=3.3.0 <4.0.0'
```

Raise SDK minimum recursively across a monorepo:

```sh
pm raise-min-sdk 3.4.0 -r
```

Raise SDK minimum recursively and opt out of tightening:

```sh
pm raise-min-sdk 3.4.0 -r --no-tighten
```

Raise SDK maximum recursively:

```sh
pm raise-max-sdk 5.0.0 -r
```
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
analyzer:
exclude:
- test/fixtures/**
Loading