Skip to content

Monorepo support (e.g. yarn workspaces) #19

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

Merged
merged 23 commits into from
Jan 6, 2021
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

.bsb.lock
lib/bs
/node_modules
node_modules
test/**/*.js
doc/tags

Expand All @@ -37,3 +37,8 @@ doc/tags
!rescript-vscode/extension/server/win32/rescript-editor-support.exe

rescript-vscode/extension/server/node_modules/.bin/

examples/**/node_modules
examples/**/lib
examples/**/src/*.js
examples/**/.merlin
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

## master

** Improvements: **

- Upgrade to `rescript-vscode@1.4.0` (see changes [here](https://github.com/rescript-lang/rescript-vscode/blob/1.0.4/HISTORY.md#104))
- Add proper monorepo support (`e.g. yarn workspaces`)
- Detects `bsb` / `bsc` correctly for each buffer separately.
- Heuristic for detecting the binaries: For the current file, find the nearest `node_modules/bs-platform` folder for the binaries
- Adds an `augroup RescriptAutoProjectEnv` that sets the environment on every `.res` / `.resi` related read / write / new file event
- Will also update the environment on each `format` and `build` call to make it sync up for all non-rescript buffers
- On each env update, it updates the local working directory to the updated project root path as well
- Fixes issue with long template strings breaking the syntax highlighting
- Fixes an issue where `:RescriptBuild` would fail in non-rescript buffers due to a wrongly scoped script variable (was buffer only)
- Add new commands `:RescriptBuildWorld` and `:RescriptCleanWorld` for cleaning / building all sources + dependencies

## 1.1.0

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Filetype detection for `.res`, `.resi`
- Basic automatic indentation
- Includes LSP for coc-vim usage
- Proper tooling detection for monorepo like setups (yarn workspaces)

**Provided by vim-rescript commands:**
- Formatting `.res` files w/ syntax error diagnostics in VIM quickfix
Expand All @@ -24,6 +25,12 @@
- Building the current projec w/ build diagnostics in VIM quickfix
- Autocompletion w/ Vim's omnicomplete

**Monorepo support:**

The vim-rescript plugin automatically updates its project environment on each file open separately.
- Tested for yarn workspaces (see [./examples/monorepo-yarn-workspaces])
- **Note for non-LSP usage:** Always make sure to switch to a `.res` file **within the project you want to compile** before running `:RescriptBuild` etc.

See `:h rescript` for the detailed [helpfile](./doc/rescript.txt).

## Requirements
Expand Down
69 changes: 45 additions & 24 deletions autoload/rescript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ function! s:ShowInPreview(fname, fileType, lines)
endif
endfunction

" Inits the plugin variables, e.g. finding all the necessary binaries
function! rescript#Init()
if has('macunix')
let b:rescript_arch = "darwin"
elseif has('win32')
let b:rescript_arch = "win32"
elseif has('unix')
let b:rescript_arch = "linux"
endif

" Configures the project related globals based on the current buffer location
" This is needed for supporting monorepos with multiple projects / multiple
" bs-platform setups
function! rescript#UpdateProjectEnv()
" Looks for the nearest node_modules directory
let l:res_bin_dir = finddir('node_modules', ".;") . "/bs-platform/" . b:rescript_arch
let l:res_bin_dir = finddir('node_modules/bs-platform/', ".;") . s:rescript_arch

"if !exists("g:rescript_compile_exe")
let g:rescript_compile_exe = l:res_bin_dir . "/bsc.exe"
Expand All @@ -42,24 +36,41 @@ function! rescript#Init()
let g:rescript_build_exe = l:res_bin_dir . "/bsb.exe"
"endif

" Needed for state tracking of the formatting error state
let s:got_format_err = 0
let s:got_build_err = 0

if !exists("g:rescript_editor_support_exe")
let g:rescript_editor_support_exe = s:rescript_plugin_dir . "/rescript-vscode/extension/server/" . b:rescript_arch . "/rescript-editor-support.exe"
endif

" Not sure why, but adding a ".;" doesn't find bsconfig when
" the editor was started without a specific file within the project
let g:rescript_project_config = findfile("bsconfig.json")
" Note that this doesn't find bsconfig when the editor was started without a
" specific file within the project
let g:rescript_project_config = findfile("bsconfig.json", ".;")

" Try to find the nearest .git folder instead
if g:rescript_project_config == ""
let g:rescript_project_root = finddir(".git/..", expand('%:p:h').';')
else
let g:rescript_project_root = fnamemodify(g:rescript_project_config, ":p:h")
endif

" Make sure that our local working directory is in the rescript_project_root
exe "lcd " . g:rescript_project_root
endfunction

" Inits the plugin variables, e.g. finding all the plugin related binaries
" and initialising some internal state for UI (error window etc.)
function! rescript#Init()
if has('macunix')
let s:rescript_arch = "darwin"
elseif has('win32')
let s:rescript_arch = "win32"
elseif has('unix')
let s:rescript_arch = "linux"
endif

" Needed for state tracking of the formatting error state
let s:got_format_err = 0
let s:got_build_err = 0

if !exists("g:rescript_editor_support_exe")
let g:rescript_editor_support_exe = s:rescript_plugin_dir . "/rescript-vscode/extension/server/" . s:rescript_arch . "/rescript-editor-support.exe"
endif

call rescript#UpdateProjectEnv()
endfunction

function! s:DeleteLines(start, end) abort
Expand All @@ -79,6 +90,7 @@ function! rescript#GetRescriptVscodeVersion()
endfunction

function! rescript#DetectVersion()
call rescript#UpdateProjectEnv()
let l:command = g:rescript_compile_exe . " -version"

silent let l:output = system(l:command)
Expand All @@ -96,6 +108,8 @@ function! rescript#DetectVersion()
endfunction

function! rescript#Format()
call rescript#UpdateProjectEnv()

let l:ext = expand("%:e")

if matchstr(l:ext, 'resi\?') == ""
Expand Down Expand Up @@ -376,8 +390,15 @@ function! rescript#Complete(findstart, base)
return l:ret
endfunction

function! rescript#BuildProject()
let out = system(g:rescript_build_exe)
function! rescript#BuildProject(...)
call rescript#UpdateProjectEnv()

let l:cmd = g:rescript_build_exe
if a:0 ==? 1
let l:cmd = g:rescript_build_exe . " " . a:1
endif

let out = system(l:cmd)

" We don't rely too heavily on exit codes. If there's a compiler.log,
" then there is either an error or a warning, so we rely on the existence
Expand Down
11 changes: 10 additions & 1 deletion doc/rescript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@ COMMANDS *rescript-commands*
after writing your new .res file, otherwise the compiler will not compile.

*:RescriptBuild*
Builds your current project with g:rescript_build_exe
Builds your current project with g:rescript_build_exe (without -make-world
flag)

*:RescriptBuildWorld*
Builds your current project with g:rescript_build_exe (with -make-world).
This is useful for building your ReScript dependencies as well.

*:RescriptCleanWorld*
Cleans all project files + all ReScript dependencies. This is useful for
fixing stale caches (e.g. when upgrading ReScript versions).

*:RescriptTypeHint*
Uses the g:rescript_editor_support_exe executable to extract
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions examples/basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "basic",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"bs-platform": "8.4.2",
"reason-react": "0.9.1"
}
}
6 changes: 6 additions & 0 deletions examples/basic/src/Button.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@react.component
let make = () => {
<div className="text-white bg-red">
{React.string("Click me")}
</div>
}
Loading