Skip to content

Allow procs to be passed to the watch method #3

@wout

Description

@wout

It would be useful if a proc could be passed as the second argument of the watch method to allow custom commands based on the name of the changed file. For example:

custom_command = ->(file : String) : String? do
  # ... build a command based on the changed file, return the string
end

Watch.watch "./**/*.cr", custom_command

An example usage could be running the respective spec file for any file in the src/ dir:

# runs matching spec for any file in /src
run_spec = ->(file : String) : String? do
  spec = file
    .sub(/^\.\/src\//, "./spec/")
    .sub(/(_spec|_flow)?\.cr$/, "_spec.cr")

  if File.exists?(spec)
    puts %(> running "#{spec}")
    "crystal spec #{spec}"
  else
    puts %(> matching spec "#{spec}" does not exist)
  end
end

Watch.watch "./**/*.cr", run_spec

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions